• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList copying  RSS feed

 
Brian Rupasinghe
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a List<List<String>> als in Java. I can copy it to another List with the following:

List<List<String>> atemp = new ArrayList<List<String>> (als);

My problem is that when i modify an element in als, it automatically changes in the other list (i.e. atemp) as well. How to avoid this?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you do the copy like you are doing, you are just copying the references that the List holds. Since the List holds more Lists, the inner Lists can be changed and both the new and old outer Lists see the change. To avoid that you will have to copy the contents of the inner Lists. In this case, you will not use the copy constructor you referenced in your post. Rather, create a new empty list, then iterate over the old List, making a new copy of each inner List and add it to the new List.
 
Brian Rupasinghe
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:When you do the copy like you are doing, you are just copying the references that the List holds. Since the List holds more Lists, the inner Lists can be changed and both the new and old outer Lists see the change. To avoid that you will have to copy the contents of the inner Lists. In this case, you will not use the copy constructor you referenced in your post. Rather, create a new empty list, then iterate over the old List, making a new copy of each inner List and add it to the new List.


Hi, here is the code i used. But still got the same problem.

for (int i=0; i<als.size();i++)
{

List><String> lst = new ArrayList<String>(als.get(i));
atemp.add(lst);
}
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show a complete runnable example that shows the problem - make an SSCCE (<- link) so that I can see the code, run the code to see the problem, then comment on it. The small snippets you show aren't enough to judge by. Also, be very clear by what 'the problem' is: what do you expect and what is different.

(p.s. You do not need to post your entire application, please just pair it down to the bare minimum required to show the problem.)
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you copy a reference to a mutable reference type, you get two names for the same reference. Changing the state of the object pointed to by variable a will always change the state of the object pointed to by reference b if they both point to the same object. That is normal behaviour. I think you call that an alias.

If you want to avoid that sort of behaviour, you would have to copy all the objects in the Lists, rather like a deep clone.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Rupasinghe wrote:Hi, here is the code i used. But still got the same problem.

Right, but how did you set up 'atemp'?

If it's as you showed us above and you simply added that loop, atemp will contain two copies of als: one shallow one (which you already know doesn't work) and another deep one AFTER the shallow one.

Since String is immutable, I don't see anything basically wrong with your loop; it just needs to be executed on an empty List.

HIH

Winston
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!