正如标题所示,将字符串数组转换为向量的最佳方法是什么?
谢谢。
调用使用现有集合(在本例中为您的数组)来初始化自身的Vector构造函数:
String[] strings = { "Here", "Are", "Some", "Strings" };
Vector<String> vector = new Vector<String>(Arrays.asList(strings));
Vector<String> strVector = new Vector<String>(Arrays.asList(strArray));
分解一下:
Arrays.asList(array)
将数组转换为一个实现了 Collection
接口的 List
。
Vector(Collection)
构造函数接受一个 Collection
,并基于它实例化一个新的 Vector
。
我们将新的 List
传递给 Vector
构造函数,以从 String
数组中获取一个新的 Vector
,然后将此对象的引用保存在 strVector
中。
new Vector(Arrays.asList(array))