【问题标题】:best way to convert an array of strings to a vector?将字符串数组转换为向量的最佳方法?
【发布时间】:2010-12-09 16:32:58
【问题描述】:

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

谢谢

【问题讨论】:

  • 有什么理由使用 Vector 而不是 ArrayList?

标签: java arrays vector


【解决方案1】:

调用使用现有集合(在本例中为您的数组)的 Vector 构造函数来初始化自身:

String[] strings = { "Here", "Are", "Some", "Strings" };
Vector<String> vector = new Vector<String>(Arrays.asList(strings));

【讨论】:

    【解决方案2】:
    Vector<String> strVector = new Vector<String>(Arrays.asList(strArray));
    

    分解:

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

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

    • 我们将新的List 传递给Vector 构造函数以从Strings 的数组中获取新的Vector,然后将对该对象的引用保存在strVector 中。

    【讨论】:

    • 所以我可以直接做 String[] myArray = {"hello", "world"} 然后在 Vector 的构造函数中抛出 Arrays.asList(myArray) ?
    【解决方案3】:
    new Vector(Arrays.asList(array))
    

    【讨论】:

      猜你喜欢
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 2019-03-29
      相关资源
      最近更新 更多