Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best way to convert an array of strings to a vector?

As the title suggests, what is the best method for converting an array of strings to a vector?

Thanks

like image 852
Julio Avatar asked Dec 09 '10 16:12

Julio


3 Answers

Call the constructor of Vector that uses an existing collection (your array, in this case) to initialize itself:

String[] strings = { "Here", "Are", "Some", "Strings" };
Vector<String> vector = new Vector<String>(Arrays.asList(strings));
like image 152
Justin Niessner Avatar answered Sep 18 '22 20:09

Justin Niessner


Vector<String> strVector = new Vector<String>(Arrays.asList(strArray));

Breaking this down:

  • Arrays.asList(array) converts the array to a List (which implements Collection)

  • The Vector(Collection) constructor takes a Collection and instantiates a new Vector based off of it.

  • We pass the new List to the Vector constructor to get a new Vector from the array of Strings, then save the reference to this object in strVector.

like image 29
Reese Moore Avatar answered Sep 17 '22 20:09

Reese Moore


new Vector(Arrays.asList(array))
like image 24
Jinesh Parekh Avatar answered Sep 16 '22 20:09

Jinesh Parekh