java 将字符串数组转换为向量的最佳方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4400642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
best way to convert an array of strings to a vector?
提问by Julio
As the title suggests, what is the best method for converting an array of strings to a vector?
正如标题所暗示的,将字符串数组转换为向量的最佳方法是什么?
Thanks
谢谢
回答by Justin Niessner
Call the constructor of Vector that uses an existing collection (your array, in this case) to initialize itself:
调用使用现有集合(在本例中为您的数组)的 Vector 构造函数来初始化自身:
String[] strings = { "Here", "Are", "Some", "Strings" };
Vector<String> vector = new Vector<String>(Arrays.asList(strings));
回答by Reese Moore
Vector<String> strVector = new Vector<String>(Arrays.asList(strArray));
Breaking this down:
打破这个:
Arrays.asList(array)
converts the array to aList
(which implementsCollection
)The
Vector(Collection)
constructor takes aCollection
and instantiates a newVector
based off of it.We pass the new
List
to theVector
constructor to get a newVector
from the array ofString
s, then save the reference to this object instrVector
.
Arrays.asList(array)
将数组转换为 aList
(实现Collection
)该
Vector(Collection)
构造函数采用Collection
和实例化一个新Vector
的基于它关闭。我们将 new 传递
List
给Vector
构造函数以Vector
从String
s的数组中获取一个 new ,然后将对此对象的引用保存在strVector
.
回答by Jinesh Parekh
new Vector(Arrays.asList(array))