Java ArrayList clone method explanation with example

Java ArrayList clone method:

The clone() method of Java ArrayList is used to do a shallow copy of an ArrayList in Java. In this post, we will learn how this method works with examples.

Definition of ArrayList.clone() method:

The clone() method is defined in java.util.ArrayList class as:

public Object clone()

This method returns a shallow copy of the ArrayList instance. Note that it is a shallow copy. This method doesn’t take any parameter and it returns a copy of the ArrayList.

Example 1: Example of ArrayList.clone() method with an integer ArrayList:

Let’s write one program that uses the ArrayList.clone() method to clone an ArrayList:

import java.util.ArrayList;

class Example {
    public static void main(String[] args) {

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

        numList.add(1);
        numList.add(2);
        numList.add(3);
        System.out.println("Given ArrayList: " + numList);

        ArrayList<Integer> cloneList = (ArrayList<Integer>) numList.clone();

        System.out.println("New cloned ArrayList: " + cloneList);
    }
}

In this example,

  • The numList is an ArrayList of integers.
  • We added three numbers to the ArrayList.
  • The clone method return an Object. So, the value is casted to an ArrayList and assigned to the cloneList variable.
  • The last line is printing the new cloned ArrayList.

If you run this program, it will print the below output:

Given ArrayList: [1, 2, 3]
New cloned ArrayList: [1, 2, 3]

Example 2: Example of ArrayList.clone() method with an ArrayList of objects:

The following example uses ArrayList.clone() method with an ArrayList of objects:

import java.util.ArrayList;

class Student {
    String name;
    int age;

    public Student(String mName, int mAge) {
        this.name = mName;
        this.age = mAge;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class Example {
    public static void main(String[] args) {

        ArrayList<Student> givenList = new ArrayList<>();

        givenList.add(new Student("Alex", 12));
        givenList.add(new Student("Daisy", 10));
        givenList.add(new Student("Bob", 11));
        System.out.println("Given ArrayList: " + givenList);

        ArrayList<Student> cloneList = (ArrayList<Student>) givenList.clone();

        System.out.println("New cloned ArrayList: " + cloneList);
    }
}

We created a class Student and three different objects of this class are added to the ArrayList givenList. The clone() method is called on this ArrayList and the returned value is assigned to the cloneList variable.

This works similar to the previous example and it will print the below output.

Given ArrayList: [Student{name='Alex', age=12}, Student{name='Daisy', age=10}, Student{name='Bob', age=11}]
New cloned ArrayList: [Student{name='Alex', age=12}, Student{name='Daisy', age=10}, Student{name='Bob', age=11}]

What will happen if we modify an item in the cloned ArrayList:

Let’s make some changes to the above example:

import java.util.ArrayList;

class Student {
    String name;
    int age;

    public Student(String mName, int mAge) {
        this.name = mName;
        this.age = mAge;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class Example {
    public static void main(String[] args) {

        ArrayList<Student> givenList = new ArrayList<>();

        givenList.add(new Student("Alex", 12));
        givenList.add(new Student("Daisy", 10));
        givenList.add(new Student("Bob", 11));

        System.out.println("Given ArrayList: " + givenList);
        ArrayList<Student> cloneList = (ArrayList<Student>) givenList.clone();
        System.out.println("New cloned ArrayList: " + cloneList);

        cloneList.get(0).setAge(10);

        System.out.println("Given ArrayList after modification: " + givenList);
        System.out.println("New cloned ArrayList after modification: " + cloneList);
    }
}
  • We added a new method setAge to the Student class that can be used to change the age of a Student object.
  • It updates the age of the first object to 10 on the cloned list.
  • Both original and cloned lists are printed after the age is changed.

If you run this program, it will print:

Given ArrayList: [Student{name='Alex', age=12}, Student{name='Daisy', age=10}, Student{name='Bob', age=11}]
New cloned ArrayList: [Student{name='Alex', age=12}, Student{name='Daisy', age=10}, Student{name='Bob', age=11}]
Given ArrayList after modification: [Student{name='Alex', age=10}, Student{name='Daisy', age=10}, Student{name='Bob', age=11}]
New cloned ArrayList after modification: [Student{name='Alex', age=10}, Student{name='Daisy', age=10}, Student{name='Bob', age=11}]

As the method clone() returns a shallow copy, once we made any changes to the cloned list, it also updates the object of the original list.

You might also like: