The clone()
method is a useful feature of the Java ArrayList
class that allows you to create a shallow copy of an existing ArrayList
instance. This is especially handy when you need a duplicate list to work with, without modifying the original list or manually copying all the elements.
What Is the clone() Method in ArrayList?
- The
clone()
method creates a shallow copy of theArrayList
. - The new list will have the same elements and the same order as the original.
- It performs a shallow copy: if the list contains references (like objects), both lists will point to the same objects.
Syntax
public Object clone()
Parameters
- The method does not take any parameters.
Return Value
- Returns a new
ArrayList
object that is a shallow copy of the original list. - The return type is
Object
, so you typically cast it:
ArrayList<Type> cloneList = (ArrayList<Type>) originalList.clone();
Exceptions
- Throws
OutOfMemoryError
if there is not enough memory (rare for normal use). - There are no checked exceptions for standard
ArrayList
.
How Does clone() Work Internally?
clone()
creates a new instance ofArrayList
with the same size and content.- For a shallow copy, primitive types (like
int
,String
) are copied as values. - For objects, only the references are copied — the cloned list contains pointers to the same objects as the original.
Examples of the clone() Method
1. Clone an ArrayList
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> colors = new ArrayList<>(); colors.add("Red"); colors.add("Green"); colors.add("Blue"); ArrayList<String> clonedColors = (ArrayList<String>) colors.clone(); System.out.println("Original: " + colors); // [Red, Green, Blue] System.out.println("Cloned: " + clonedColors); // [Red, Green, Blue] } }
Output:
Original: [Red, Green, Blue]
Cloned: [Red, Green, Blue]
2. clone() with a List of Objects
import java.util.ArrayList; class Person { String name; Person(String name) { this.name = name; } } public class Demo { public static void main(String[] args) { ArrayList<Person> list1 = new ArrayList<>(); list1.add(new Person("Alice")); list1.add(new Person("Bob")); ArrayList<Person> list2 = (ArrayList<Person>) list1.clone(); // Changing object in list2 also reflects in list1 (shallow copy) list2.get(0).name = "Eve"; System.out.println(list1.get(0).name); // Output: Eve System.out.println(list2.get(0).name); // Output: Eve } }
Note: Since clone()
is a shallow copy, both lists share the same objects.
Important Notes
- Shallow Copy: Only the references in the list are copied, not the actual objects. Changes to shared objects will be seen in both the original and the clone.
- Type Safety: Always cast the result back to
ArrayList<Type>
. - Deep Copy: For deep copying (copying the objects themselves), you must manually clone/copy each object in the list.
Summary
Aspect | Details |
---|---|
Method Purpose | To create a shallow copy of an ArrayList |
Syntax | clone() |
Return Value | Object (cast to ArrayList<Type> ) |
Parameters | None |
Copy Type | Shallow |
Typical Use Cases | Duplicating a list for separate operations, avoiding unwanted changes to the original during manipulations |
Typical Use Cases
- Undo functionality: Keep old states of a list by cloning before changes.
- Simulations: Work on duplicates to prevent altering the source data.
- Testing: Easily create test copies for modifications.