views:

130

answers:

5

How can I convert in the quickest way String[] to Vector<String>?

+11  A: 

With Arrays.asList :

Vector<String> v = new Vector<String>(Arrays.asList(yourStringArray));

Resources :

Colin Hebert
Beat me to it, and provided a better answer using typed collections as well as javadoc reference.
tschaible
Note that `Arrays.asList()` will throw if the array is `null`.
gpeche
+1, and there's your 2nd "Enlightened" badge. Congrats :)
Bozho
Well, thank you :)
Colin Hebert
+1  A: 
Vector v = new Vector( Arrays.asList(array) );
tschaible
+1  A: 

Like so:

Vector<String> strings = new Vector<String>( Arrays.asList(yourStringArray ) );

Shakedown
+1  A: 

Check this example : http://www.java2s.com/Code/Java/Collections-Data-Structure/ConvertanArraytoaVector.htm

It will look like :

    String[] arr = { "1", "2", "3", "4" };
    Vector<String> v = new Vector<String>(Arrays.asList(arr));
YoK
+1  A: 

As mentioned above you can get a List created for you using Arrays.asList and then forcefully create a Vector. But, my question is if you absolutely need to use a Vector. Vector is synchronized http://download.oracle.com/javase/6/docs/api/java/util/Vector.html and you might not want this overhead. But, if you need synchronization would you consider developing againt the interface List and then use Collections.synchronizedList to achieve the synchronization? http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#synchronizedList%28java.util.List%29

If you are using an API which requires the type Vector then this is of course not a choice...

Alex VI