Copy All List Items to Vector in Java
This example shows how to create or copy a Vector with a collection of objects that inherits from the
java.util.List. In this code, by using the
addAll(), you can copy to another collection.
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class Copyallelements {
public static void main(String[] args) {
Vector< String> vct = new Vector< String> ();
//add elements
vct.add("first");
vct.add("second");
vct.add("third");
vct.add("fourth");
System.out.println("Before copy:");
for(String e:vct)
System.out.println(e);
List< String> list = new ArrayList< String> ();
list.add("un");
list.add("two");
vct.addAll(list);
System.out.println("\nAfter copying: ");
for(String e:vct)
System.out.println(e);
}
}
Runtime:
Before copy:
first
second
third
fourth
After copying:
first
second
third
fourth
one
two