Java ArrayList clone() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

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.

Java
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
CallWhat 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
// java.util.ArrayList<E>
public Object clone()

// ArrayList implements Cloneable

Parameters

ParameterDescription
Noneclone() takes no parameters.

Return Value

Return ValueDescription
ObjectA new ArrayList with the same elements. Cast it to ArrayList<E>.

Compare with Similar Methods

Ways to copy list data:

MethodHow 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

Example 1 — Clone a List and Change the Copy

Add to and replace in the clone, then compare both lists.

Java
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));
    }
}

How It Works

  1. Cast needed. clone() returns Object, so it is cast back to ArrayList<String>; the annotation silences the unchecked warning.
  2. Separate storage. The clone has its own internal array, so add and set on copy do not touch original.
  3. Strings are safe. String objects cannot change, so sharing them between the two lists is harmless.
  4. 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.

Java
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));
    }
}

How It Works

  1. One builder, two lists. After clone(), both lists hold a reference to the same StringBuilder.
  2. Change is shared. Appending " v2" through drafts changes that single object, so backup shows it too.
  3. Deep copy. The loop builds a new StringBuilder from each element, so deep holds its own objects.
  4. 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.

Java
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));
    }
}

How It Works

  1. Declared as ArrayList. clone() is only callable because original has type ArrayList; on a List variable it would not compile.
  2. Two copies. The cast copy and the copy constructor both take a snapshot of 1, 2, 3.
  3. Clear the original. original.clear() empties only the original list; both copies keep their three elements.
  4. 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

Shallow. The new list has its own storage, but the element objects themselves are shared.
clone() is declared to return Object, so you must cast it to ArrayList<E>. The cast causes an unchecked warning.
Most code uses new ArrayList<>(list): it needs no cast, gives no warning, and works on any Collection.
No. clone() is not part of the List interface. Declare the variable as ArrayList or use the copy constructor.
Create a new list and add a copy of each element, for example new StringBuilder(sb) for each StringBuilder.

Next: contains()

Check whether the list holds a value.

contains() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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