29

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

3 Answers 3

66

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.

Sign up to request clarification or add additional context in comments.

3 Comments

complexity is o(n) ?
@amdev: Yes, exactly.
@JonSkeet Thank you, this helped.. I dint want to go with Iterator approach and all because of large chunk of data.. hence this helped.. better than iterating and populating the arrayList
2

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.