Forums > Java

Copying ArrayList data, not references

February 9, 2007 | 7:39 pm

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);

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:


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.


February 9, 2007 | 7:56 pm

On 07-02-09, at 1939, jbmaxwell wrote:
> I hope that title makes some sort of sense…
yep, you need to deep copy.

hope that helps.


February 9, 2007 | 8:09 pm

Cool, I’ll look into that. It’s amazing how far you can go and not ever have to do something so simple… weird.



February 10, 2007 | 10:21 am

Brilliant. That was the problem. Fixed!


Viewing 4 posts - 1 through 4 (of 4 total)