How to convert Vector to String array in java

﹥>﹥吖頭↗ 提交于 2019-11-30 11:12:54
NPE

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

try this example

  Vector token
  String[] criteria = new String[tokenVector.size()];
  tokenVector.toArray(criteria);

Try this:

vector.toArray(new String[0]);

Edit: I just tried it out, new String[vector.size()] is slower then new String[0]. So ignore what i said before about vector.size(). String[0] is also shorter to write anyway.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!