开发者

java vector to arraylist

Is there any way to copy or convert a vector to arraylis开发者_StackOverflow社区t in Java?


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.


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);
    }
}


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

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜