Add the object to the ArrayList and change it later.

If I have an ArrayList and I added an object to it, and later I changed this object, will this change reflect in ArrayList? or when I add an object to ArrayList, does Java create a copy and add it to ArrayList?

What if I change the reference to this object to null? Does this mean that the object in the ArrayList array is also null?

+56
java arraylist
Aug 16 2018-11-11T00:
source share
4 answers

Will this change reflect in ArrayList?

Yes, since you added a link to the object in the list. The added link will still point to the same object (which you changed).


or when I add an object to ArrayList, does Java create a copy and add it to ArrayList?

No, it will not copy the object. (He will copy the link to the object.)


What if I change the reference to this object to null? Does this mean that the object in the ArrayList is now also null?

No, because the contents of the original link were copied when added to the list. (Keep in mind that this is a copy, not an object.)

Demonstration:

StringBuffer sb = new StringBuffer("foo"); List<StringBuffer> list = new ArrayList<StringBuffer>(); list.add(sb); System.out.println(list); // prints [foo] sb.append("bar"); System.out.println(list); // prints [foobar] sb = null; System.out.println(list); // still prints [foobar] 
+68
Aug 16 2018-11-18T00:
source share

Any change to the object will be reflected in the list.

However, when you are dealing with objects that are immutable, a new object will be created in “change operations”. Than in fact your old object is still in the list, and you have a new one elsewhere.

+4
Aug 16 2018-11-11T00:
source share

Thanks to everyone guys. I realized this again by reading your posts. This is probably a confusing concept since I have digested it for a long time, but recently I forgot how it works again. So I want to share a key that solves the problem for me. (note that non-primitive Objects (int primitives, logical, etc.) in Java are technically pointers). When you add the o object to the list, the list and o point to the same thing, so when you change o, the list item changes. This is as long as they point to the same thing and break when o points to something else with = .

 o = null; //o points to nothing and changes in o from now on doesn't effect the list item 

or

 Object a = new Object(); o = a; //o and the list item don't point to same thing so changes in o doesn't effect the list item (but it effects a) 

hope this helps someone

0
Dec 14 '18 at 9:12
source share

One more demonstration is required where the ArrayList is inside the Map as a value. ArrayList changes after adding to Map and Map reflects the changes.

There is one element in the card with the name of the mother as the key, and the children with the value.

  String key = "adeleMom"; Map<String, ArrayList<String>> myMap = new HashMap<String, ArrayList<String>>(); ArrayList<String> firstList = new ArrayList<String>(); firstList.add("adele"); myMap.put(key, firstList); firstList = null; ArrayList secondList = myMap.get(key); System.out.println(secondList); // prints [adele] secondList.add("bonnie"); System.out.println("Added bonnie"); ArrayList thirdList = myMap.get(key); System.out.println(thirdList); // prints [adele, bonnie] 
-one
Jan 15 '15 at 17:45
source share



All Articles