Copying ArrayList data, not references
I hope that title makes some sort of sense...
I have to preface this by mentioning that I'm not even certain I've found the bug in my class, but I *think* I've found it.
I've got two Maps that will have different ways of using the contents of an single ArrayList. The ArrayList is all fine and happy as a value inside Map1. What I want to do is get the data from the ArrayList in Map1, move it over to Map2, and arrange it a little differently. I did a simple:
newArrayList = Map1.get(keyToOldArrayList);
Map2.put(newKey,newArrayList);
as a way of moving the data over. However, when I alter Map2, oldArrayList in Map1 gets altered as well. This is no big surprise, but how do I avoid it? I tried:
newArrayList.addAll(Map1.get(keyToOldArrayList);
but that has the same result (or so it seems).
BTW, if this is the way I'm supposed to copy the data, in order to make a copy I can manipulate without messing with Map1, then I've got another problem somewhere else and I'll leave everybody alone. I just can't see anywhere else that could be giving me this sort of trouble.
J.
On 07-02-09, at 1939, jbmaxwell wrote:
>
> I hope that title makes some sort of sense...
>
yep, you need to deep copy.
http://java.sun.com/developer/JDCTechTips/2001/tt0410.html
http://www.faqs.org/docs/think_java/TIJ319.htm
http://www.google.com/search?q=java+arraylist+deep+copy
hope that helps.
r.
Cool, I'll look into that. It's amazing how far you can go and not ever have to do something so simple... weird.
thanks.
J.
Brilliant. That was the problem. Fixed!
J.