coder247
coder247

Reputation: 2943

java vector to arraylist

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

Upvotes: 28

Views: 32363

Answers (3)

Javier
Javier

Reputation: 9

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

Upvotes: 0

Kevin Parker
Kevin Parker

Reputation: 17216

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

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503839

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.

Upvotes: 67

Related Questions