import java.util.ArrayList;Runtime:
public class ArrayList_clone {
public static void main(String[] args) {
ArrayList< String> al = new ArrayList< String> ();
//Add Items to ArrayList
al.add("Pen");
al.add("Kit");
al.add("Pencil");
al.add("Calculator");
System.out.println("ArrayList: "+al);
ArrayList< String> al2 = (ArrayList< String>) al.clone();
System.out.println("Cloned ArrayList: "+ al2);
//add and remove items from the original list
al.add("Notebook");
al.remove("Pencil");
//Show both lists after adding and deleting
System.out.println("Original ArrayList: "+al);
System.out.println("Cloned ArrayList: "+al2);
}
}
ArrayList: [Pen, Kit, Pencil, Calculator]However, you should consider not using the clone() method. It works very well with collections, but generally, it's better to use the builder new ArrayList.
Cloned ArrayList: [Pen, Kit, Pencil, Calculator]
ArrayList Original: [Pen, Kit, Calculator, Notebook]
Cloned ArrayList: [Pen, Kit, Pencil, Calculator]
Please disable your ad blocker and refresh the window to use this website.