将字符串数组转换为矢量的最佳方法?
答
呼叫向量的使用现有的集合(您的数组,在这种情况下)的构造函数初始化自己:
String[] strings = { "Here", "Are", "Some", "Strings" };
Vector<String> vector = new Vector<String>(Arrays.asList(strings));
+0
+1。我认为这比使用泛型的职位要好。 – 2010-12-09 16:37:33
答
Vector<String> strVector = new Vector<String>(Arrays.asList(strArray));
打破下来:
Arrays.asList(array)
将数组转换为List
(其实现TSCollection
)的
Vector(Collection)
构造需要Collection
和基于实例关闭它一个新的Vector
。我们通过新的
List
到Vector
构造函数来得到的String
在数组新Vector
,然后保存在strVector
引用此对象。
+0
所以我可以直接做String [] myArray = {“hello”,“world”},然后在Vector的构造函数中引入Arrays.asList(myArray)? – Julio 2010-12-09 16:37:45
答
new Vector(Arrays.asList(array))
您使用的是矢量,而不是一个ArrayList的原因吗? – Daniel 2010-12-09 16:58:36