How to convert Vector to String array in java

后端 未结 7 1635
被撕碎了的回忆
被撕碎了的回忆 2021-01-01 17:58

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

相关标签:
7条回答
  • 2021-01-01 18:39

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

    0 讨论(0)
  • 2021-01-01 18:43

    try this example

      Vector token
      String[] criteria = new String[tokenVector.size()];
      tokenVector.toArray(criteria);
    
    0 讨论(0)
  • 2021-01-01 18:45
    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.

    0 讨论(0)
  • 2021-01-01 18:57

    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.

    0 讨论(0)
  • 2021-01-01 18:59

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

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

    0 讨论(0)
  • 2021-01-01 19:02

    Vector.ToArray(T[])

    0 讨论(0)
提交回复
热议问题