w3resource

Java ArrayList.clone() Method

public Object clone()

The clone() method is used to create a new instance of an ArrayList object that is a shallow copy of an existing ArrayList object.

Package: java.util

Java Platform: Java SE 8

Syntax:

clone()

Return Value:
A clone of this ArrayList instance

Pictorial presentation of ArrayList.clone() Method

Java ArrayList.clone() Method

Example: ArrayList.clone Method

The following example returns a shallow copy of this LinkedList.

import java.util.*;

public class test {
  public static void main(String[] args) {
 
   // ArrayList with Capacity 4
      ArrayList<String> StudentList = new ArrayList<String>(4);
	   Object cloneList;
      //Added 4 elements
       StudentList.add("David");
       StudentList.add("Tom");
       StudentList.add("Rohit");
       StudentList.add("Paul");
      
       System.out.println("Elements in StudentList are: ");
       System.out.println(StudentList);
  
       cloneList = StudentList.clone();
       System.out.println("Elements in cloneList are:");
       System.out.println(cloneList);
   }
}

Output:

F:\java>javac test.java
F:\java>java test
Elements in StudentList are:
[David, Tom, Rohit, Paul]
Elements in cloneList are:
[David, Tom, Rohit, Paul]

Java Code Editor:

Previous:lastIndexOf Method
Next:toArray Method



Follow us on Facebook and Twitter for latest update.