0

I have two different arraylist of objects

ArrayList<Buckets> parent;
ArrayList<Buckets> child;

Now I want to initialize all the objects of child with parent's value using

child = new ArrayList<>(parent);

But after this whenever I make any changes in child, the parent arraylist also get updated. How can I make the child arraylist independent. I checked the other answers mostly are using collection but its not working here either. Can anyone kindly help me? thank you

  • 1
    You will have to copy each element individually. Does Your `Bucket` class define a copy constructor which does *deep-copying*? – TheLostMind Nov 17 '15 at 18:20
  • @VinodMadyalkar no i was copying it using the arraylist constructor – user2924551 Nov 17 '15 at 18:23
  • That would create a *new* `ArrayList` with different references to the same Objects as the Original List. Thus changing the value using this List will also be reflected in the Original List. – TheLostMind Nov 17 '15 at 18:25

0 Answers0