Try your Java power.
See the code below. I'm trying to assign "D" to every value in an arrayList.
ExtentionForSample.java
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
for(String str : arrayList) {
str = "D";
System.out.println(str);
}
System.out.println(arrayList);
D
D
D
[A, B, C]
I just changed the reference destination of the local variable. .. ..
I was just assigning a new reference to a local variable. Does not affect the referenced "A"
ExtentionForSample.java
//Omission
for(int i=0; i < arrayList.size(); i++) {
arrayList.set(i, "D");
}
System.out.println(arrayList);
Use ArrayList.set (int index, E element)
[D, D, D]
ExtentionForSample.java
Map mapA = new HashMap<String,String>(){{put("1", "A");}};
Map mapB = new HashMap<String,String>(){{put("1", "B");}};
Map mapC = new HashMap<String,String>(){{put("1", "C");}};
arrayListMap.add(mapA);
arrayListMap.add(mapB);
arrayListMap.add(mapC);
for(Map map:arrayListMap) {
map.put("2", "D");
}
System.out.println(arrayListMap);
[{1=A, 2=D}, {1=B, 2=D}, {1=C, 2=D}]
When data is manipulated for the reference destination assigned to the local variable, it is reflected in the reference destination.
Recommended Posts