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:
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:
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/collection/java-collection-exercise-16.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics