Java - How to Convert a Vector to an ArrayList
This example shows how to convert from Vector to ArrayList. We proceed as follows: create a Vector that contains Strings and then create an ArrayList from this Vector.In the following code, we have a Vector that contains String objects and we are in the process of converting it into an ArrayList of Strings. First, we create a Vector filled with String elements and second, the step of converting from Vector to ArrayList using the Vector object as an argument in the declaration of the ArrayList.
import java.util.ArrayList;Runtime:
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;
public class VectorToArrayList {
public static void main(String[] args) {
Vector< String> vec = new Vector< String> ();
//insert elements
vec.add("o1");
vec.add("o2");
vec.add("o3");
System.out.println("Vector Elements:");
for(String e:vec)
System.out.println(e);
//ArrayList declarioin initialized with Vector
List< String> arraylist = new ArrayList< String> (vec);
System.out.println("ArrayList:"Elements);
for(String e:arraylist)
System.out.println(e);
}
}
Vector Elements:
o1
o2
o3
ArrayList:
o1
o2
o3