开发者

java vector to arraylist

开发者 https://www.devze.com 2023-01-10 15:30 出处:网络
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:

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

精彩评论

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