clone() Method : Java.util.ArrayList
Declaration :
1 |
public Object clone() |
Explanation :
Purpose | Method returns a shallow copy of this ArrayList instance |
Parameters | NA |
Return Value | Clone of this ArrayList instance |
Exception | NA |
Example
The following example shows the usage of java.util.ArrayList.clone() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.c4learn.arraylist; import java.util.ArrayList; public class ArrayListExamples { public static void main(String args[]) { // Create ArrayList 1 ArrayList<String> arrlist1 = new ArrayList<String>(5); // Fill Elements in ArrayList 1 arrlist1.add("A"); arrlist1.add("B"); arrlist1.add("C"); // Print Elements in ArrayList 1 System.out.println("ArrayList 1 : " + arrlist1); // Clone Operation ArrayList<String> arrlist2 = (ArrayList<String>) arrlist1.clone(); System.out.println("ArrayList 2 : " + arrlist2); } } |
Output :
1 2 |
ArrayList 1 : [A, B, C] ArrayList 2 : [A, B, C] |