The objects in the List were references, right? Confirmation of

Memorandum on processing the contents of the List

Tested because I felt that the objects in the List were not references

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)

Conclusion

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.

Postscript

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

The objects in the List were references, right? Confirmation of
Extract a specific element from the list of objects
A program that counts the number of words in a List
[Java] Delete the elements of List
Output the difference between each field of two objects in Java
Sort a List of Java objects
Official logo list of the service
List of members added in Java 9
Examine the list of timezone IDs available in the Java ZoneId class
List of types added in Java 9
Order of processing in the program
What you are doing in the confirmation at the time of gem update
Extract a specific element from the list of objects
Confirmation and refactoring of the flow from request to controller in [httpclient]
Get the result of POST in Java
The identity of params [: id] in rails
The story of AppClip support in Anyca
Easily measure the size of Java Objects
How to sort the List of SelectItem
The story of writing Java in Emacs
Write the movement of Rakefile in the runbook
Get the path defined in Controller class of Spring boot as a list
The list of installed apps is not displayed in getPackageManager (Android11 ​​/ API level 30)
[No.004] Corrected the order list screen of the orderer
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
List of methods used in PAIZA D rank
Add empty data to the top of the list
About the idea of anonymous classes in Java
The story of learning Java in the first programming
Measure the size of a folder in Java
Feel the passage of time even in Java
Import files of the same hierarchy in Java