How to Make a Deep Copy of Java Arraylist

How to make a deep copy of Java ArrayList

Cloning the objects before adding them. For example, instead of newList.addAll(oldList);

for(Person p : oldList) {
newList.add(p.clone());
}

Assuming clone is correctly overriden inPerson.

how to deep copy one arraylist into another

As I mentioned in my comments to your question, the clone() method returns an Object, and since the compiler can see and know only this, you must (dangerously) cast the object returned to be ArrayList for the method to work:

arrli.add((ArrayList<Integer>) arrli1.clone());

Having said this, I implore you not to use clone() in this situation and that you read Why people are so afraid of using clone() (on collection and JDK classes)? to see why.

If this were my code, I'd make new ArrayLists objects within the for loop rather than cloning existing ones. I'm not 100% sure of what you're trying to achieve here, but perhaps something similar to:

// list to hold other lists
List<List<Integer>> listOfLists = new ArrayList<>();

// inner list to be added to above list
List<Integer> innerList = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {

// if we need to create a new inner list, add old to
// list of lists and create new one
if (i != 0 && arr[i] < arr[i - 1]) {
listOfLists.add(innerList);
innerList = new ArrayList<>();
}

// always add to the current inner list
innerList.add(arr[i]);
}

// when all is done, add last inner list to list of lists
listOfLists.add(innerList);

This above code looks to be cleaner and safer. Also, if you're wondering why I'm using:

List<Integer> foo = new ArrayList<>();

rather than

ArrayList<Integer> foo = new ArrayList<>();

Please check out:

  • Why do some people use the List base class to instantiate a new ArrayList? as well as
  • What does it mean to “program to an interface”?

How to clone ArrayList and also clone its contents?

You will need to iterate on the items, and clone them one by one, putting the clones in your result array as you go.

public static List<Dog> cloneList(List<Dog> list) {
List<Dog> clone = new ArrayList<Dog>(list.size());
for (Dog item : list) clone.add(item.clone());
return clone;
}

For that to work, obviously, you will have to get your Dog class to implement the Cloneable interface and override the clone() method.

Deep copy of a list with custom objects in java

IMO, add another arg constructor, which will do deep cloning for your Object.

public class MyMemo {
MyMemo(MyMemo memo)
{
this.commentText = memo.getCommentText();
this.imageUriList = new ArrayList<>();
for (Uri uri : memo. getImageUriList())
{
this.imageUriList.add(new Uri(uri));
}
}
// rest of your code
}

Now, iterate your parent List and clone each object

List<MyMemo> parentList = new ArrayList<>();
List<MyMemo> copyList = new ArrayList<>();
for (MyMemo memo : parentList)
{
// create new instance of MyMemo and add to the list
copyList.add(new MyMemo(memo));
}


Related Topics



Leave a reply



Submit