×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Added: May 5, 2021 3:20 PM
Modified: May 5, 2021 3:23 PM
Views: 3985
Tags: collections
  1.  public static void main(String[] args) {
  2.  
  3.  
  4.         //ArrayList anlegen
  5.         ArrayList<String> colors = new ArrayList<>();
  6.         //Neue ArrayList anlegen
  7.         ArrayList<String> newcolors = new ArrayList<>();
  8.         //Elemente mit .add hinzufügen
  9.         colors.add("Red");
  10.         colors.add("Green");
  11.         colors.add("Orange");
  12.         colors.add("White");
  13.         colors.add("Black");
  14.  
  15.  
  16.         newcolors.add("1");
  17.         newcolors.add("2");
  18.         newcolors.add("3");
  19.         newcolors.add("4");
  20.         newcolors.add("5");
  21.  
  22.  
  23.         System.out.println("Vorher: " + colors);
  24.         //Neuen String anlegen
  25.         String new_color = "White";
  26.         //Aber für bessere Ausgabe, durchiterieren!
  27.         //Zweiten Index Ersetzen durch neue Farbe!
  28.         colors.set(1, new_color);
  29.        
  30.  
  31.         System.out.println("Replace second element with 'White': ");
  32.         System.out.println(colors);
  33.     }
  34. }