将字符串数组转换为矢量的最佳方法?

问题描述:

正如标题所示,将字符串数组转换为向量的最佳方法是什么?将字符串数组转换为矢量的最佳方法?

感谢

+1

您使用的是矢量,而不是一个ArrayList的原因吗? – Daniel 2010-12-09 16:58:36

呼叫向量的使用现有的集合(您的数组,在这种情况下)的构造函数初始化自己:

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(其实现TS Collection

  • Vector(Collection)构造需要Collection和基于实例关闭它一个新的Vector

  • 我们通过新的ListVector构造函数来得到的String在数组新Vector,然后保存在strVector引用此对象。

+0

所以我可以直接做String [] myArray = {“hello”,“world”},然后在Vector的构造函数中引入Arrays.asList(myArray)? – Julio 2010-12-09 16:37:45

new Vector(Arrays.asList(array))