How to clone ArrayList and also clone its contents

0 votes
how can I Create a duplicate of the arraylist in java
Jun 5, 2018 in Java by sharth
• 3,370 points
550 views

1 answer to this question.

0 votes

Iterate on the items, and clone them one after the other,  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 object to implement the Cloneable interface, and the clone() method

answered Jun 5, 2018 by Akrati
• 3,190 points

Related Questions In Java

0 votes
0 answers
0 votes
2 answers

How do I read and convert an InputStream object to string type?

You can also use Java Standard Library ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,920 points
17,228 views
0 votes
2 answers

When to use LinkedList and ArrayList in Java?

ArrayList is what you want. LinkedList is almost always a ...READ MORE

answered Dec 11, 2018 in Java by Sushmita
• 6,920 points
1,140 views
0 votes
2 answers

How to convert byte array to String and STring to byte array?

The best possible way of conversion between byte[] and String is to ...READ MORE

answered Aug 22, 2019 in Java by Sirajul
• 59,230 points
3,601 views
+1 vote
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
1,185 views
0 votes
2 answers
0 votes
2 answers

Remove duplicate elements from an ArrayList in Java

List<String> al = new ArrayList<>(); // add elements ...READ MORE

answered Jul 26, 2018 in Java by Sushmita
• 6,920 points
1,676 views
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,920 points
1,910 views
0 votes
2 answers

How to convert an array to arraylist?

In Java 9 you can use: List<String> list= List.of("Hello", "World", ...READ MORE

answered Aug 1, 2018 in Java by samarth295
• 2,220 points
1,053 views
0 votes
1 answer

How can we convert Strings to and from UTF8 byte arrays?

To convert STring to byte[]: String s = ...READ MORE

answered Jun 21, 2018 in Java by Akrati
• 3,190 points
1,847 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP