Memorandum on processing the contents of the List
Prepare a test class
public class Test {
public static void main(String[] args) {
TestEntity enttity = new TestEntity();
TestEntity enttity2 = new TestEntity();
TestEntity enttity3 = new TestEntity();
ArrayList<TestEntity> list = new ArrayList<>();
list.add(enttity);
list.add(enttity2);
list.add(enttity3);
showEntityList(list);
}
static void showEntityList(ArrayList<TestEntity> list) {
System.out.println("Look at the contents of the entity");
for (TestEntity a : list) {
System.out.println(a.a);
System.out.println(a.b);
System.out.println(a.c);
System.out.println(a.d);
}
}
}
public class TestEntity {
String a = "start";
String b = "b";
String c = "c";
String d = "d";
}
Execution result
Look at the contents of the entity
start
b
c
d
start
b
c
d
start
b
c
d
When you touch the contents of the extracted object
Prepare method
/**
*Just take out the entity and process it
* */
static void modifyOnly(ArrayList<TestEntity> list) {
int size = list.size();
for (int i=0;i<size; i++) {
TestEntity value = list.get(i);
value.a = "Hajima Rides(" + i + ")";
value.b = "2(" + i + ")";
value.c = "3(" + i + ")";
value.d = "4(" + i + ")";
}
}
Try using the above
TestEntity enttity = new TestEntity();
TestEntity enttity2 = new TestEntity();
TestEntity enttity3 = new TestEntity();
ArrayList<TestEntity> list = new ArrayList<>();
list.add(enttity);
list.add(enttity2);
list.add(enttity3);
modifyOnly(list);
showEntityList(list);
Execution result
Look at the contents of the entity
Hajima Rides(0)
2(0)
3(0)
4(0)
Hajima Rides(1)
2(1)
3(1)
4(1)
Hajima Rides(2)
2(2)
3(2)
4(2)
Just taking it out and processing the contents will affect the contents of the List.
It's strange, if I did this at work, it wouldn't be rewritten, and if I set the rewritten entity again, it did what I wanted. Well, this movement is exactly what I expected.
Well, it's a staring contest with the source of the workplace.
Thanks to @shiracamus, I learned how to make the source on Qiita easier to read. Thank you for not knowing how to use Qiita before technical matters.
You can point out and merge. However, it is a secret (sweat) that when I checked the contents of the indication after fixing it by hand without knowing it, it was in a merged state and on the contrary the correction became strange.
Recommended Posts