w3resource

Java: Clone an array list to another array list

Java Collection, ArrayList Exercises: Exercise-16 with Solution

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

Pictorial Presentation:

Java Collection, ArrayList Exercises: Clone an array list to another array list

Sample Solution:-

Java Code:

import java.util.ArrayList;
import java.util.Collections;
  public class Exercise16 {
  public static void main(String[] args) {
          ArrayList<String> c1= new ArrayList<String>();
          c1.add("Red");
          c1.add("Green");
          c1.add("Black");
          c1.add("White");
          c1.add("Pink");
          System.out.println("Original array list: " + c1);
          ArrayList<String> newc1 = (ArrayList<String>)c1.clone();
          System.out.println("Cloned array list: " + newc1);       
}
}

Sample Output:

Note: Exercise16.java uses unchecked or unsafe operations.              
Note: Recompile with -Xlint:unchecked for details.                     
Original array list: [Red, Green, Black, White, Pink]                  
Cloned array list: [Red, Green, Black, White, Pink]

Flowchart:

Flowchart: Clone an array list to another array list.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Join two array lists.
Next: Empty an array list.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.