【发布时间】:2010-12-09 16:32:58
【问题描述】:
正如标题所示,将字符串数组转换为向量的最佳方法是什么?
谢谢
【问题讨论】:
-
有什么理由使用 Vector 而不是 ArrayList?
正如标题所示,将字符串数组转换为向量的最佳方法是什么?
谢谢
【问题讨论】:
调用使用现有集合(在本例中为您的数组)的 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) 将数组转换为List(实现Collection)
Vector(Collection) 构造函数采用Collection 并基于它实例化一个新的Vector。
我们将新的List 传递给Vector 构造函数以从Strings 的数组中获取新的Vector,然后将对该对象的引用保存在strVector 中。
【讨论】:
new Vector(Arrays.asList(array))
【讨论】: