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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:12:38  来源:igfitidea点击:

best way to convert an array of strings to a vector?

javaarraysvector

提问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 a List(which implements Collection)

  • The Vector(Collection)constructor takes a Collectionand instantiates a new Vectorbased off of it.

  • We pass the new Listto the Vectorconstructor to get a new Vectorfrom the array of Strings, then save the reference to this object in strVector.

  • Arrays.asList(array)将数组转换为 a List(实现Collection

  • Vector(Collection)构造函数采用Collection和实例化一个新Vector的基于它关闭。

  • 我们将 new 传递ListVector构造函数以VectorStrings的数组中获取一个 new ,然后将对此对象的引用保存在strVector.

回答by Jinesh Parekh

new Vector(Arrays.asList(array))