ArrayList.clone()

To clone or make a shallow copy of an ArrayList, call clone() method on the given ArrayList.

In this tutorial, we will learn about the Java ArrayList.clone() method, and learn how to use this method to make a shallow copy of this ArrayList, with the help of examples.

clone()

ArrayList.clone() returns a shallow copy of this ArrayList instance.

ADVERTISEMENT

Syntax

The syntax of clone() method is

ArrayList.clone()

Returns

The method returns Object which can be type-casted to ArrayList.

Example 1 – clone()

In this example, we will create an ArrayList initialized with String values, and clone this ArrayList using clone() method.

Java Program

import java.util.Arrays;
import java.util.ArrayList;

public class Example {  
	public static void main(String[] args) {  
		ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList("a", "b", "c"));
		System.out.println("Original ArrayList : " + arrayList);
		
		ArrayList<String> arrayListCloned = (ArrayList<String>) arrayList.clone();
		System.out.println("Cloned ArrayList   : " + arrayListCloned);
    }  
}

Output

Original ArrayList : [a, b, c]
Cloned ArrayList   : [a, b, c]

Any modifications done to the cloned ArrayList will not reflect in the original ArrayList, since these original and cloned ArrayList s are two different instances.

In the following program, we will add elements to the cloned ArrayList arrayListCloned, and this should not affect the original arrayList.

Java Program

import java.util.Arrays;
import java.util.ArrayList;

public class Example {  
	public static void main(String[] args) {  
		ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList("a", "b", "c"));
		System.out.println("Original ArrayList : " + arrayList);
		
		ArrayList<String> arrayListCloned = (ArrayList<String>) arrayList.clone();
		arrayListCloned.add("d");
		arrayListCloned.add("e");
		System.out.println("Cloned ArrayList   : " + arrayListCloned);
    }  
}

Output

Original ArrayList : [a, b, c]
Cloned ArrayList   : [a, b, c, d, e]

Example 2 – clone() – ArrayList with User-defined Objects

In this example, we will define and initialize a ArrayList with objects of type Car. We will clone this ArrayList, using clone() method. And following this, we will provide another program to understand the “shallow copy”.

Java Program

import java.util.ArrayList;

public class Example {  
	public static void main(String[] args) {
		Car car1 = new Car(1, "Tesla");
		Car car2 = new Car(2, "BMW");
		Car car3 = new Car(3, "Toyota");
		
		ArrayList<Car> arrayList = new ArrayList<Car>();
		arrayList.add(car1);
		arrayList.add(car2);
		arrayList.add(car3);
		System.out.println("Original ArrayList : " + arrayList);
		
		ArrayList<Car> arrayListCloned = (ArrayList<Car>) arrayList.clone();
		System.out.println("Cloned   ArrayList : " + arrayListCloned);
    }  
}

class Car {
	int id;
	String name;
	public Car(int id, String name) {
		this.id = id;
		this.name = name;
	}
	
	@Override
	public String toString() {
		return id + "-" + name;
	}
}

Output

Original ArrayList : [1-Tesla, 2-BMW, 3-Toyota]
Cloned   ArrayList : [1-Tesla, 2-BMW, 3-Toyota]

If any of the Car objects is modified, the changes effect both original ArrayList and cloned ArrayList.

In the following program, we will modify the name of Car in car3, and let us observe how this effects original ArrayList and cloned ArrayList.

Java Program

import java.util.ArrayList;

public class Example {  
	public static void main(String[] args) {
		Car car1 = new Car(1, "Tesla");
		Car car2 = new Car(2, "BMW");
		Car car3 = new Car(3, "Toyota");
		
		ArrayList<Car> arrayList = new ArrayList<Car>();
		arrayList.add(car1);
		arrayList.add(car2);
		arrayList.add(car3);
		
		ArrayList<Car> arrayListCloned = (ArrayList<Car>) arrayList.clone();

		car3.name = "Audi";
		
		System.out.println("Original ArrayList : " + arrayList);
		System.out.println("Cloned   ArrayList : " + arrayListCloned);
    }  
}

class Car {
	int id;
	String name;
	public Car(int id, String name) {
		this.id = id;
		this.name = name;
	}
	
	@Override
	public String toString() {
		return id + "-" + name;
	}
}

Output

Original ArrayList : [1-Tesla, 2-BMW, 3-Audi]
Cloned   ArrayList : [1-Tesla, 2-BMW, 3-Audi]

Hence, the shallow copy by clone() method.

Conclusion

In this Java Tutorial, we have learnt the syntax of Java ArrayList.clone() method, and also learnt how to use this method with the help of examples.