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

后端 未结 3 408
攒了一身酷
攒了一身酷 2020-12-31 19:03

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

Thanks

相关标签:
3条回答
  • 2020-12-31 19:46
    new Vector(Arrays.asList(array))
    
    0 讨论(0)
  • 2020-12-31 19:48

    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));
    
    0 讨论(0)
  • 2020-12-31 19:52
    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.

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