.clone()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Jun 24, 2024
Contribute to Docs

In Java, the .clone() method returns a cloned version of the given ArrayList. The cloned version is a shallow copy containing references to all the elements in the original ArrayList, not the elements themselves.

Syntax

arrayList.clone();
  • arrayList: The ArrayList to be cloned.

Example

The following example illustrates how to use the .clone() method.

import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// Creating an ArrayList
ArrayList<Integer> nums = new ArrayList<>();
// Adding elements to the ArrayList
nums.add(12);
nums.add(34);
nums.add(56);
// Printing the ArrayList
System.out.println("Original ArrayList: " + nums);
// Cloning the ArrayList
ArrayList<Integer> clonedNums = (ArrayList<Integer>)nums.clone();
// Printing the cloned ArrayList
System.out.println("Cloned ArrayList: " + clonedNums);
}
}

Since the .clone() method returns an Object, it needs to be cast to ArrayList<Integer> to convert it into an ArrayList of integers.

The above example produces the following output:

Original ArrayList: [12, 34, 56]
Cloned ArrayList: [12, 34, 56]

All contributors

Looking to contribute?

Learn Java on Codecademy