Java Arraylist Clone() Function

clone method in java

What is an ArrayList?

Here, in the page we will discuss about the ArrayList’s Clone() Method in java.
Arraylist is a part of collection framework in java. we can perform several inbuilt functions of java on arraylist like clone() Function which can be found in the java.util.cloning Package.It implements the List interface of the collections framework.

ArrayList add Function:

Java contains several inbuilt functions that are used to perform different operations on arraylist. clone() function is also an inbuilt method of Java class cloning.
The clone Function is used to make a clone or copy of an object of ArrayList Class. cloning is a process of making a duplicate of an object. 

Syntax:

arraylist1 = arraylist2.clone()

Definition of Parameters:

arrayList1 : This is the new object of class ArrayList in which we want to copy our existing arraylist object.
arrayList2 : This is the  cloning object of class ArrayList.

Return Type

It returns a shallow copy of this ArrayList instance.

Example1:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        // Initializing the new Arraylist
        ArrayList arr1 = new ArrayList<>();

        // Adding elements to the Arraylist
        arr1.add(1);
        arr1.add(2);
        arr1.add(3);
        arr1.add(4);

        // printing the arraylist
        System.out.println("Initial Arraylist Object : " + arr1);

        // Initializing the new arraylist
        ArrayList arr2 = new ArrayList<>();

        // Cloning the first Arraylist
        arr2 = (ArrayList)arr1.clone();

        // Printing the cloned ArrayList
        System.out.println("Clonned Arraylist Object : " + arr2);
    }
}

Output:

Initial Arraylist Object : [1, 2, 3, 4]
Clonned Arraylist Object : [1, 2, 3, 4]
clone method

Explanation:

In the above example, we are making a clone of ArrayList object arr1 and arr2 is the name of the cloned object. we can also directly print the cloned object

Example2:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        // Initializing the new Arraylist
        ArrayList arr1 = new ArrayList<>();

        // Adding Elements to the ArrayList
        arr1.add(1);
        arr1.add(2);
        arr1.add(3);
        arr1.add(4);

        // Printing the Arraylist
        System.out.println("Initial Arraylist Object : " + arr1);
                
        // printing the cloned object
        System.out.println("Clonned Arraylist Object : " + arr1.clone());
    }
}

Output:

Initial Arraylist Object : [1, 2, 3, 4]
Clonned Arraylist Object : [1, 2, 3, 4]

Explanation:

In the above Example. we are directly printing the Cloned object of ArrayList class arr1 without storing its value to any other object.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription