Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

Java ArrayList clone() Method

Posted on July 22, 2025 By Admin No Comments on Java ArrayList clone() Method

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 the ArrayList.
  • 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 of ArrayList 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

AspectDetails
Method PurposeTo create a shallow copy of an ArrayList
Syntaxclone()
Return ValueObject (cast to ArrayList<Type>)
ParametersNone
Copy TypeShallow
Typical Use CasesDuplicating 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.
Java Tags:Java-ArrayList, Java-ArrayList-Method, Java-Collections

Post navigation

Previous Post: Java ArrayList clear() Method
Next Post: Java ArrayList contains() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Java ArrayList
  • Java ArrayList Methods
  • Java ArrayList add() Method
  • Java ArrayList addAll() Method
  • Java ArrayList clear() Method
  • Java ArrayList clone() Method
  • Java ArrayList contains() Method
  • Java ArrayList ensureCapacity() Method
  • Java ArrayList forEach() Method
  • Java ArrayList get() Method
  • Java ArrayList indexOf() Method
  • Java ArrayList isEmpty() Method
  • Java ArrayList iterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList listIterator() Method

Recent Posts

  • Java ArrayList remove() Method
  • Java ArrayList listIterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList iterator() Method
  • Java ArrayList isEmpty() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme