Copy Java Vector To Java ArrayList Using Collection
Chapter:
Collections
Last Updated:
11-06-2016 19:52:42 UTC
Program:
/* ............... START ............... */
import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;
public class JavaCopyVectorToArrayList {
public static void main(String[] args) {
// create a Vector object
Vector v = new Vector();
// Add elements to Vector
v.add("1");
v.add("2");
v.add("3");
// create an ArrayList object
ArrayList arrayList = new ArrayList();
// Add elements to Arraylist
arrayList.add("Java");
arrayList.add("Scan");
arrayList.add("Scan");
arrayList.add("Scan");
System.out.println("Before copy : " + arrayList);
Collections.copy(arrayList, v);
System.out.println("After Copy : " + arrayList);
}
}
/* ............... END ............... */
Output
Before copy : [Java, Scan, Scan, Scan]
After Copy : [1, 2, 3, Scan]
Notes:
-
Copy all elements of Vector to ArrayList using copy method of Collections class.
Tags
Collection,Java Vector To ArrayList, Java