Copy Elements of One ArrayList to Another ArrayList in Java
Description
The following code shows how to copy Elements of One ArrayList to Another ArrayList.
Example
/*from w w w .j a va 2 s. co m*/
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList1 = new ArrayList<String>();
arrayList1.add("1");
arrayList1.add("2");
arrayList1.add("3");
ArrayList<String> arrayList2 = new ArrayList<String>();
arrayList2.add("One");
arrayList2.add("Two");
arrayList2.add("Three");
arrayList2.add("Four");
arrayList2.add("java2s.com");
System.out.println(arrayList2);
Collections.copy(arrayList2, arrayList1);
System.out.println(arrayList2);
}
}
The code above generates the following result.