Java Reference Library →

ArrayList clone() method - Syntax and Examples

ArrayList clone() method - Syntax and Examples

Learn about the Java ArrayList clone() method, its syntax, parameters, return value, and practical examples. Understand how to create a deep copy of an ArrayList.

This reference explains ArrayList clone() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

Introduction

Sometimes, you need to create a completely independent copy of an ArrayList. Modifying the original list shouldn't affect the copy, and vice versa. The clone() method provides a way to achieve this by creating a shallow copy.

Syntax

    public Object clone()
    

Parameters

ParameterDescription
NoneThe clone() method takes no parameters.

Return Value

The clone() method returns an object that is a copy of the original ArrayList.

Important Notes: Shallow vs Deep Copy

Understanding the difference between a shallow and deep copy is crucial. The clone() method performs a shallow copy. This means it creates a new ArrayList object, but the elements within the list are still references to the same objects as in the original list. If those elements are mutable (changeable), changes made to them through either list will affect both lists.

Examples

Example 1: Basic Cloning

This example demonstrates the basic usage of the clone() method. We create an ArrayList, clone it, and verify that they are different objects in memory.

    import java.util.ArrayList;

public class CloneExample1 {
    public static void main(String[] args) {
        ArrayList<String> originalList = new ArrayList<>();
        originalList.add("Apple");
        originalList.add("Banana");

        ArrayList<String> clonedList = (ArrayList<String>) originalList.clone(); // Explicit casting is required.

        System.out.println("Original List: " + originalList);
        System.out.println("Cloned List: " + clonedList);

        System.out.println("Are they the same object? " + (originalList == clonedList)); // Checks if they point to the same memory location.
    }
}
    
Original List: [Apple, Banana]
Cloned List: [Apple, Banana]
Are they the same object? false

The output shows that originalList and clonedList are distinct objects. The cast to ArrayList<String> is necessary because `clone()` returns an `Object`.

Example 2: Demonstrating Shallow Copy

This example highlights the shallow copy behavior. We create an ArrayList of mutable objects (integers wrapped in Integer), clone it, and then modify one of the elements in the original list to illustrate that the change is reflected in both lists.

    import java.util.ArrayList;

public class CloneExample2 {
    public static void main(String[] args) {
        ArrayList<Integer> originalList = new ArrayList<>();
        originalList.add(10);
        originalList.add(20);

        ArrayList<Integer> clonedList = (ArrayList<Integer>) originalList.clone();

        System.out.println("Original List before modification: " + originalList);
        System.out.println("Cloned List before modification: " + clonedList);

        originalList.set(0, 15); // Modifying the element at index 0 in the original list.

        System.out.println("Original List after modification: " + originalList);
        System.out.println("Cloned List after modification: " + clonedList);
    }
}
    
Original List before modification: [10, 20]
Cloned List before modification: [10, 20]
Original List after modification: [15, 20]
Cloned List after modification: [15, 20]

Notice that changing the value in originalList (from 10 to 15) also changed the corresponding element in clonedList. This is because both lists hold references to the same Integer objects.

Example 3: Creating a Deep Copy

To perform a deep copy (creating completely independent copies of the elements), you'll need to iterate through the original list and create new instances of each element. This example shows how to do it for an ArrayList of Strings.

    import java.util.ArrayList;

public class CloneExample3 {
    public static void main(String[] args) {
        ArrayList<String> originalList = new ArrayList<>();
        originalList.add("Hello");
        originalList.add("World");

        ArrayList<String> deepCopiedList = new ArrayList<>();
        for (String str : originalList) {
            deepCopiedList.add(new String(str)); // Create a new String object.
        }

        System.out.println("Original List: " + originalList);
        System.out.println("Deep Copied List: " + deepCopiedList);

        originalList.set(0, "Goodbye"); // Modify the original list

        System.out.println("Original List after modification: " + originalList);
        System.out.println("Deep Copied List after modification: " + deepCopiedList);
    }
} 
    
Original List: [Hello, World]
Deep Copied List: [Hello, World]
Original List after modification: [Goodbye, World]
Deep Copied List after modification: [Hello, World]

In this deep copy example, a new String object is created for each element in the original ArrayList. Therefore, modifications to originalList do not affect deepCopiedList.

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →