Convert a Vector to an array in Java
You can copy all elements from a Vector to an array by passing the created array object through the
copyInto().
import java.util.Vector;
public class VectorToArray {
public static void main(String[] args) {
Vector< String> vct = new Vector< String> ();
//add elements
vct.add("un");
vct.add("two");
vct.add("three");
vct.add("four");
System.out.println("Vector:");
for(String e:vct)
System.out.println(e);
String[] copyArr = new String[vct.size()];
vct.copyInto(copyArr);
System.out.println("Copied Items:");
for(String e:copyArr){
System.out.println(e);
}
}
}
Runtime:
Vector:
one
two
three
four
Copied Items:
one
two
three
four