I have a list that I am trying to fill with numbers from a different List
. However, when I change the number in the list I copied from, it changes them in the other list. Is there any way to stop this from happening?

- 58,613
- 19
- 146
- 147

- 279
- 2
- 6
- 16
-
4Could we see some code? Seeing how you're trying to do it right now can help us give you the most useful answer to your question. – MattS Jun 27 '12 at 18:46
-
you make only a shallow copy. http://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy – duffy356 Jun 27 '12 at 18:48
-
2can you give us more specifics of the objects your using? – Frank Visaggio Jun 27 '12 at 18:48
-
Collections.copy(List1, List2); You can change the values in list1 and it won't affect list2 – Akhi Jun 27 '12 at 18:45
3 Answers
You need to copy the values in your list into a new list:
List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = new ArrayList<String>(list1); // copy the values of list1 into list2
That will copy all of the values in list1
into list2
. Then you can make changes to list2
without modifying list1
and vice versa.
Just to clarify:
That will not do a deep copy. However, this is not an issue of deep/shallow copying. The issue here is that the OP was passing his list into another method and then was surprised to learn that his list had had numbers added/removed.

- 27,191
- 6
- 63
- 65
-
2Doesn't collections.copy do a shallow copy too? You need a deep copy if you want to have separate objects. It also depends which implementation of the List interface is he using... Check this out: http://stackoverflow.com/questions/689370/java-collections-copy-list-i-dont-understand – Fritz Jun 27 '12 at 18:50
-
1@TimPote, if he had primitives he wouldn't have this problem since they are immutable... – dacwe Jun 27 '12 at 18:53
-
Indeed. The OP should be using immutable primitives, or doing the deep copy explicitly. – Louis Wasserman Jun 27 '12 at 19:01
-
@LouisWasserman My initial interpretation was that the OP was passing a reference to his List to another method and then was surprised that *his* List had had numbers add/removed as a result. Now I'm just unsure about what exactly he means. I await more information from the OP. – Tim Pote Jun 27 '12 at 19:05
-
I didn't assume he was using primitives to I expected the worst case. He's going to need to clone() the objects if his list doesn't store primitives. But indeed, we require a little extra info. – Fritz Jun 27 '12 at 19:06
-
This answer has several problems: definition of copy(List super T> dest, List extends T> src), so list1 should be source. After swap list1 and list2, the copy will throw IndexOutOfBoundsException. So it won't work either. – dragon66 Jun 27 '12 at 19:14
-
@dragon66 Correct on both counts. Silly me for assuming `Collections.copy` works as advertised... See my edits. – Tim Pote Jun 27 '12 at 19:19
The two Lists contain references to the same number objects. Change a number in the first list, and you're changing the same object referenced by the second list.
To maintain two Lists with separate independent copies of the numbers, you'll need to copy the List, e.g. with Collections.copy
.

- 58,613
- 19
- 146
- 147
-
Now that I'm re-reading it I'm a little unsure about what his issue is. I initially thought he was using the same List (i.e. passing his reference to another method). I see how you interpreted it however, and it's unclear what exactly's going on. – Tim Pote Jun 27 '12 at 19:01
You need to do deep copy on list's object.
use Collections.copy(List1, List2);
or write your custom code.

- 1
- 1

- 40,646
- 13
- 77
- 103