Untitled Document

Subscribe here to get updates on new Videos


21. Vector class details?




previous homenext

1. The underlying Data structure for the vector is resizable array or growable array.
2. Duplicate objects are allowed.
3. Insertion order is preserved.
4. 'null' insertion is possible.
5. Heterogeneous objects are allowed.
6. Vector class implemented Serializable, Cloneable and RandomAccess interfaces.
7. Most of the methods present in Vector are synchronized. Hence Vector object is Thread-safe.
8. Best choice if the frequent operation is retrieval.

Vector Specific methods:

For adding objects:

1. add(Object o) [from collection – List(I)]
2. add(int index, Object o) [from List]
3. addElement(Object o) [from Vector]
4. For removing Objects:
5. Remove(Object o) [from collection]
6. removeElement(Object o) [from Vector]
7. remove(int index) [from List)
8. RemoveElementAt(int index) [from collection]
9. removeAllElements() [from Vector]
For Accessing Elements:
Object get(int index) [from Collection]
Object elementAt(int index) [from Vector]
Object firstElement() [from Vector]
Object lastElement() [from Vector]
Other Methods:
int size();
int capacity();

Constructors of vector class:

1. Vector v=new Vector();

Creates an empty vector object with default initial capacity 10, once vector reaches it’s max capacity a new vector object will be created with new capacity =2*current capacity.

2. Vector v=new Vector(int initialCapacity);

creates an empty Vector Object with specified initial capacity.

3. Vector v=new Vector(int initialCapacity, int incrementalCapacity);

4. Vector v=new Vector(Collection c);

Creates an equivalent Vector object for the given collection.