java 向量到数组列表

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

java vector to arraylist

javavectorarraylist

提问by coder247

Is there any way to copy or convert a vector to arraylist in Java?

有没有办法在 Java 中将向量复制或转换为数组列表?

采纳答案by Jon Skeet

Yup - just use the constructor which takes a collection as its parameter:

是的 - 只需使用以集合作为参数的构造函数:

Vector<String> vector = new Vector<String>();
// (... Populate vector here...)
ArrayList<String> list = new ArrayList<String>(vector);

Note that it only does a shallow copy.

请注意,它只执行浅拷贝。

回答by Kevin Parker

I just wrote a class to do the same thing, but is more flexible as it will accept Objects accordingly.

我只是写了一个类来做同样的事情,但更灵活,因为它会相应地接受对象。

public class ExteriorCastor {
    public static  ArrayList vectorToArrayList(Vector vector){
        if (vector == null){return null;}
        return new ArrayList<Object>(vector);
    }
}

回答by Javier

i′m not sure if it is length()or size().... but the idea is the next:

我不确定它是length()还是size()......但接下来的想法是:

ArrayList<Object> a;

for(int i = 0;i < Vector.length() ; i++)

    a.add(Vector.elementAt(i); // Again... i′m not sure if this is elementAt() or get()

Vector.finalize();