How to convert Vector with string to String array in java?
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.
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.
Try Vector.toArray(new String[0])
.
P.S. Is there a reason why you're using Vector
in preference to ArrayList
?
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.
here is the simple example
Vector<String> v = new Vector<String>();
String [] s = v.toArray(new String[v.size()]);
simplest method would be String [] myArray = myVector.toArray(new String[0]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With