3

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

1
  • 5
    why do you want to convert it to Vector and not to List? Commented Sep 11, 2010 at 11:57

5 Answers 5

15

With Arrays.asList :

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

Resources :

Sign up to request clarification or add additional context in comments.

3 Comments

Beat me to it, and provided a better answer using typed collections as well as javadoc reference.
Note that Arrays.asList() will throw if the array is null.
Does Arrays.asList(T ..) respect the element's order?
2

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...

Comments

1
Vector v = new Vector( Arrays.asList(array) );

Comments

1

Like so:

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

Comments

1

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.