Write a Java program to clone an array list to another array list


The program starts by creating an ArrayList named list_col and adding some String elements to it using the add() method.

Next, the program uses the clone() method to create a new ArrayList named clone_new which is an exact copy of list_col.

Finally, the program prints out the original list_col ArrayList and the cloned clone_new ArrayList using the println() method.

Source Code

import java.util.ArrayList;
import java.util.Collections;
public class Clone_ArrayList
{
	public static void main(String[] args)
	{
		ArrayList<String> list_col = new ArrayList<String>();
		list_col.add("Pink");
		list_col.add("Yellow");
		list_col.add("Black");
		list_col.add("White");
		list_col.add("Blue");
		list_col.add("Orange");
		System.out.println("Given Array List : " + list_col);
		ArrayList<String> clone_new = (ArrayList<String>)list_col.clone();
		System.out.println("Cloned Array List : " + clone_new);       
	}
}

Output

Given Array List : [Pink, Yellow, Black, White, Blue, Orange]
Cloned Array List : [Pink, Yellow, Black, White, Blue, Orange]

Example Programs