Java 将向量转换为列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1973705/
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-08-13 01:57:42  来源:igfitidea点击:

convert vector to list

javalistvector

提问by sarah

How to convert a vector to a list?

如何将向量转换为列表?

采纳答案by cletus

Vectoris a concrete class that implements the Listinterface so technically it is already a List. You can do this:

Vector是一个实现List接口的具体类,所以从技术上讲它已经是一个List. 你可以这样做:

List list = new Vector();

or:

或者:

List<String> list = new Vector<String>();

(assuming a Vectorof Strings).

(假设 aVectorStrings)。

If however you want to convert it to an ArrayList, which is the closest Listimplementation to a `Vector~ in the Java Collections Framework then just do this:

但是,如果您想将其转换为ArrayList,这是ListJava 集合框架中最接近`Vector~ 的实现,那么只需执行以下操作:

List newList = new ArrayList(vector);

or for a generic version, assuming a Vectorof Strings:

或者对于通用版本,假设 aVectorStrings:

List<String> newList = new ArrayList<String>(vector);

回答by les2

If you want a utility method that converts an generic Vector type to an appropriate ArrayList, you could use the following:

如果您想要一个将通用 Vector 类型转换为适当的 ArrayList 的实用方法,您可以使用以下内容:

public static <T> ArrayList<T> toList(Vector<T> source) {
    return new ArrayList<T>(source);
}

In your code, you would use the utility method as follows:

在您的代码中,您将按如下方式使用实用程序方法:

public void myCode() {
    List<String> items = toList(someVector);
    System.out.println("items => " + items);
}

You can also use the built-in java.util.Collections.list(Enumeration) as follows:

您还可以使用内置的 java.util.Collections.list(Enumeration) 如下:

public void myMethod() {
    Vector<String> stringVector = new Vector<String>();
    List<String> theList = Collections.list(stringVector.elements());
    System.out.println("theList => " + theList);
}

But like someone mentioned below, a Vector is-a List! So why would you need to do this? Perhaps you don't want some code you use to know it's working with a Vector - perhaps it is inappropriately down-casting and you wish to eliminate this code-smell. You could then use

但是就像下面提到的某人一样,Vector is-a List!那么为什么需要这样做呢?也许您不希望您使用的某些代码知道它正在与 Vector 一起工作 - 也许它是不恰当的向下转换,并且您希望消除这种代码味道。然后你可以使用

// the method i give my Vector to can't cast it to Vector
methodThatUsesList( Collections.unmodifiableList(theVector) );

if the List should be modified. An off-the-cuff mutable wrapper is:

如果列表应该被修改。即用型可变包装器是:

public static <T> List<T> asList(final List<T> vector) {
    return new AbstractList<T>() {
        public E get(int index) { return vector.get(index); }
        public int size() { return vector.size(); }
        public E set(int index, E element) { return vector.set(index, element); }
        public void add(int index, E element) { vector.add(index, element); }
        public E remove(int index) { return vector.remove(index); }
    }
}