Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Vector to String array in java

Tags:

java

vector

How to convert Vector with string to String array in java?

like image 466
jijo thomas Avatar asked Sep 21 '11 13:09

jijo thomas


People also ask

How do I turn a Vector into a String?

Convert Vector to String using toString() function To convert elements of a Vector to Strings in R, use the toString() function. The toString() is an inbuilt R function used to produce a single character string describing an R object.

Can I convert array to String in java?

Below are the various methods to convert an Array to String in Java: Arrays. toString() method: Arrays. toString() method is used to return a string representation of the contents of the specified array.


4 Answers

Try Vector.toArray(new String[0]).

P.S. Is there a reason why you're using Vector in preference to ArrayList?

like image 104
NPE Avatar answered Sep 26 '22 06:09

NPE


Vector<String> vector = new Vector<String>();
String[] strings = vector.toArray(new String[vector.size()]);

Note that it is more efficient to pass a correctly-sized array new String[vector.size()] into the method, because in this case the method will use that array. Passing in new String[0] results in that array being discarded.

Here's the javadoc excerpt that describes this

Parameters:
a - the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.

like image 45
Bohemian Avatar answered Sep 27 '22 06:09

Bohemian


here is the simple example

Vector<String> v = new Vector<String>();
String [] s = v.toArray(new String[v.size()]);
like image 3
Pratik Avatar answered Sep 28 '22 06:09

Pratik


simplest method would be String [] myArray = myVector.toArray(new String[0]);

like image 3
mcfinnigan Avatar answered Sep 26 '22 06:09

mcfinnigan