Java ArrayList clone() Method



Description

The Java ArrayList clone() returns a shallow copy of this ArrayList instance. This cloning ensures that there is no side-effect while copying and modifying the copy of arraylist.

Declaration

Following is the declaration for java.util.ArrayList.clone() method

public Object clone()

Parameters

NA

Return Value

This method returns a clone of this ArrayList instance.

Exception

NA

Cloning an ArrayList of Integers Example

The following example shows the usage of Java ArrayList clone() method. In this example, we're using Integers. At first, we're creating a arrayList as a new ArrayList object and then initialize with few items. As next step, we're cloning the arrayList1 to arrayList2 using clone() method call on arrayList1 object. In the end, we're printing arrayList2 to check if it contains copy of all elements of arrayList2 object.

package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty arrayList
      ArrayList<Integer> arrayList1 = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList1.add(1);
      arrayList1.add(2);
      arrayList1.add(3);
      arrayList1.add(4);
         
      // clone the first arrayList,
      ArrayList<Integer> arrayList2 = (ArrayList<Integer>)arrayList1.clone();

      // let us print all the elements available in arrayList2
      // now arrayList2 should have similar elements to arrayList1.
      System.out.println("ArrayList = " + arrayList2);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

ArrayList = [1, 2, 3, 4]

Cloning an ArrayList of Strings Example

The following example shows the usage of Java ArrayList clone() method. In this example, we're using Strings. At first, we're creating a arrayList as a new ArrayList object and then initialize with few items. As next step, we're cloning the arrayList1 to arrayList2 using clone() method call on arrayList1 object. In the end, we're printing arrayList2 to check if it contains copy of all elements of arrayList2 object.

package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty arrayList
      ArrayList<String> arrayList1 = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList1.add("A");
      arrayList1.add("B");
      arrayList1.add("C");
      arrayList1.add("D");
         
      // clone the first arrayList,
      ArrayList<String> arrayList2 = (ArrayList<String>)arrayList1.clone();

      // let us print all the elements available in arrayList2
      // now arrayList2 should have similar elements to arrayList1.
      System.out.println("ArrayList = " + arrayList2);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

ArrayList = [A, B, C, D]

Cloning an ArrayList of Objects Example

The following example shows the usage of Java ArrayList clone() method. In this example, we're using Student objects. At first, we're creating a arrayList as a new ArrayList object and then initialize with few items. As next step, we're cloning the arrayList1 to arrayList2 using clone() method call on arrayList1 object. In the end, we're printing arrayList2 to check if it contains copy of all elements of arrayList2 object.

package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty arrayList
      ArrayList<Student> arrayList1 = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList1.add(new Student(1, "Julie"));
      arrayList1.add(new Student(2, "Robert"));
      arrayList1.add(new Student(3, "Adam"));
         
      // clone the first arrayList,
      ArrayList<Student> arrayList2 = (ArrayList<Student>)arrayList1.clone();

      // let us print all the elements available in arrayList2
      // now arrayList2 should have similar elements to arrayList1.
      System.out.println("ArrayList = " + arrayList2);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

ArrayList = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_arraylist.htm
Advertisements