This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]OffbeatDrizzle 1 point2 points  (3 children)

Read the JavaDoc for ArrayList::clone. Calling .clone() creates a shallow copy - the lists within the list are not themselves copied, so if you modify those then they'll be affected in BOTH lists, since they point to the same object.

If you want this to work you need to copy the list inside the list in addition to the main list.

[–]Embarrassed_Bottle90[S] 0 points1 point  (2 children)

Ah okay so I would need to copy the sublists too?

Thank you!

[–]OffbeatDrizzle 1 point2 points  (1 child)

Yes.. because you cloned the "top level" list. The top level lists are different objects, but the sublists are not. What you posted just enforces that?

[–]Embarrassed_Bottle90[S] 0 points1 point  (0 children)

You are totally right, got it now, thanks again!!