• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList copying

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic