Question

Since they are both classes of the Collection Interface class, can I declare a vector and display it as an arrayList? My professor says I can but I really don't see how that would work. Is she wrong or is there really a way to display a vector as an arrayList? This is just me being curious.

Was it helpful?

Solution

Let's look at the two:

public class Vector<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable

public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable

These are two seperate classes and cannot be interchanged. You can do the following:

AbstractList<String> = new ArrayList<String>();
AbstractList<String> = new Vector<String>();

List<String> = new ArrayList<String>();
List<String> = new Vector<String>();

If you want to form an ArrayList from a vector or vice-versa, you can do the following:

Vector<String> newVector = new Vector<String>(oldArrayList);
ArrayList<String> newArrayList= new ArrayList<String>(oldVector);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top