How to clone ArrayList and also clone its contents?

0 votes
how can I Create a duplicate of the arraylist in java
Jun 4, 2018 in Java by sharth
• 3,350 points
51 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 4, 2018 by Akrati
• 3,150 points

Related Questions In Java

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,880 points
4,443 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,880 points
137 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 in Java by Sirajul
• 38,420 points
93 views
0 votes
1 answer

How to read and write on an excel files?

Follow these steps: Write: public class User { ...READ MORE

answered May 15, 2018 in Java by Rishabh
• 3,540 points
263 views
0 votes
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,280 points
67 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,880 points
143 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,880 points
624 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,190 points
52 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,150 points
34 views