简体   繁体   English

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

[英]best way to convert an array of strings to a vector?

As the title suggests, what is the best method for converting an array of strings to a vector? 正如标题所示,将字符串数组转换为向量的最佳方法是什么?

Thanks 谢谢

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));
Vector<String> strVector = new Vector<String>(Arrays.asList(strArray));

Breaking this down: 打破这个:

  • Arrays.asList(array) converts the array to a List (which implements Collection ) Arrays.asList(array)将数组转换为List (实现Collection

  • The Vector(Collection) constructor takes a Collection and instantiates a new Vector based off of it. Vector(Collection)构造函数接受一个Collection并基于它实例化一个新的Vector

  • We pass the new List to the Vector constructor to get a new Vector from the array of String s, then save the reference to this object in strVector . 我们将新的List传递给Vector构造函数,从String的数组中获取一个新的Vector ,然后在strVector保存对该对象的strVector

new Vector(Arrays.asList(array))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM