Home Java ArrayList Methods clone() Java ArrayList clone() Method Definition In Java, clone() returns a new ArrayList holding the same elements, in the same order, as the original. Adding, removing, or replacing elements in one list does not affect the other.
The copy is shallow : both lists point at the same element objects. If an element is mutable (such as a StringBuilder), changing that object is visible through both lists. Also, clone() returns Object, so you need a cast, which the compiler reports as an unchecked warning.
An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe .
Syntax Call it on a variable whose type is ArrayList, then cast.
ArrayList<String> original = new ArrayList<>(Arrays.asList("a", "b"));
@SuppressWarnings("unchecked")
ArrayList<String> copy = (ArrayList<String>) original.clone();
List<String> copy2 = new ArrayList<>(original); // common alternative Call What it does (ArrayList<T>) list.clone()A shallow copy; needs a cast. new ArrayList<>(list)A shallow copy without a cast; works for any collection. list.subList(a, b)A view of part of the list, not a copy. copy.add(x)Changes only the copy.
clone() is not part of the List interface. If your variable is declared as List<T>, you cannot call it without casting to ArrayList first; use new ArrayList<>(list) instead.
Library declarations // java.util.ArrayList<E>
public Object clone()
// ArrayList implements Cloneable Parameters Parameter Description None clone() takes no parameters.
Return Value Return Value Description ObjectA new ArrayList with the same elements. Cast it to ArrayList<E>.
Compare with Similar Methods Ways to copy list data:
Method How it differs new ArrayList<>(list)The usual shallow copy; no cast, no warning. subList(a, b)A live view of a range, not a copy. toArray()Copies the elements into an array. addAll(c)Copies elements into a list you already have.
Java Examples 1. Copy and change 2. Shallow copy 3. clone vs constructor Example 1 — Clone a List and Change the Copy Add to and replace in the clone, then compare both lists.
import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListCloneDemo {
public static void main(String[] args) {
ArrayList<String> original = new ArrayList<>(Arrays.asList("rock", "jazz"));
@SuppressWarnings("unchecked")
ArrayList<String> copy = (ArrayList<String>) original.clone();
copy.add("blues");
copy.set(0, "pop");
System.out.println("original: " + original);
System.out.println("copy: " + copy);
System.out.println("same object? " + (original == copy));
}
} original: [rock, jazz]
copy: [pop, jazz, blues]
same object? false How It Works Cast needed. clone() returns Object, so it is cast back to ArrayList<String>; the annotation silences the unchecked warning.Separate storage. The clone has its own internal array, so add and set on copy do not touch original.Strings are safe. String objects cannot change, so sharing them between the two lists is harmless.Two lists. original == copy is false: clone created a new ArrayList object.Example 2 — Shallow Copy: Mutable Elements Are Shared Clone a list of StringBuilder objects, change one, then make a deep copy.
import java.util.ArrayList;
public class ArrayListCloneShallow {
public static void main(String[] args) {
ArrayList<StringBuilder> drafts = new ArrayList<>();
drafts.add(new StringBuilder("draft"));
@SuppressWarnings("unchecked")
ArrayList<StringBuilder> backup = (ArrayList<StringBuilder>) drafts.clone();
drafts.get(0).append(" v2");
System.out.println("backup sees: " + backup.get(0));
System.out.println("same element? " + (backup.get(0) == drafts.get(0)));
ArrayList<StringBuilder> deep = new ArrayList<>();
for (StringBuilder sb : drafts) {
deep.add(new StringBuilder(sb));
}
drafts.get(0).append(" final");
System.out.println("deep copy keeps: " + deep.get(0));
}
} backup sees: draft v2
same element? true
deep copy keeps: draft v2 How It Works One builder, two lists. After clone(), both lists hold a reference to the same StringBuilder.Change is shared. Appending " v2" through drafts changes that single object, so backup shows it too.Deep copy. The loop builds a new StringBuilder from each element, so deep holds its own objects.Unaffected. The later " final" goes only into the original builder; the deep copy still reads "draft v2".Example 3 — clone() vs the Copy Constructor Copy the same list both ways, clear the original, and compare the copies.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayListCloneVsCopy {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
ArrayList<Integer> original = new ArrayList<>(Arrays.asList(1, 2, 3));
List<Integer> viaClone = (List<Integer>) original.clone();
List<Integer> viaConstructor = new ArrayList<>(original);
original.clear();
System.out.println("original: " + original);
System.out.println("viaClone: " + viaClone);
System.out.println("viaConstructor: " + viaConstructor);
System.out.println("equal copies? " + viaClone.equals(viaConstructor));
}
} original: []
viaClone: [1, 2, 3]
viaConstructor: [1, 2, 3]
equal copies? true How It Works Declared as ArrayList. clone() is only callable because original has type ArrayList; on a List variable it would not compile.Two copies. The cast copy and the copy constructor both take a snapshot of 1, 2, 3.Clear the original. original.clear() empties only the original list; both copies keep their three elements.Same contents. List.equals compares elements in order, so the two copies are equal. The constructor needs no cast, which is why most code prefers it.Frequently Asked Questions Is ArrayList clone() a deep or shallow copy? Shallow. The new list has its own storage, but the element objects themselves are shared.
Why does clone() need a cast? clone() is declared to return Object, so you must cast it to ArrayList<E>. The cast causes an unchecked warning.
Should I use clone() or new ArrayList<>(list)? Most code uses new ArrayList<>(list): it needs no cast, gives no warning, and works on any Collection.
Can I call clone() on a List variable? No. clone() is not part of the List interface. Declare the variable as ArrayList or use the copy constructor.
How do I make a deep copy of an ArrayList? Create a new list and add a copy of each element, for example new StringBuilder(sb) for each StringBuilder.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion