Vector in Java

Posted on Updated on

Vector implements dynamic array. Now it is evident from its definition that its size grows automatically like in ArrayList.
Prior to Java 2 i.e. before the concept of Collection was derived, Vector was used as a dynamic array. With the release of Java 2, Vector was redesigned to implement AbstractList and extend List.
It is similar to ArrayList but not exaclty same. It is different from ArrayList in two ways, as listed below:

  1. Vector is synchronized, i.e. it is thread safe.
  2. It contains legacy methods that are not part of Collection. We’ll discuss about later in this tutorial.

Vector has four constructors, as listed below:

Constructor Description
Vector() This will create an empty vector with size 10.
Vector(int size) This will create an empty vector with initial capacity defined by size.
Vector(int size, int incr) This will create an empty vector with initial capacity defined by size and post that increment by incr.
Vector(Collection c) This will create a vector with elements of Collection c.

A vector is initially created with some capacity, post it is occupied the size increases as required plus some extra room. This extra space added is to avoid the repetitive memory allocation. This is important because allocation are costly in terms of time. If the increment size is not defined then size is doubled each allocation cycle.

Vector defines these protected data members:

Data Member Description
int capacityIncrement The increment value is stored in capacityIncrement.
int elementCount The number of elements currently in the vector is stored in elementCount.
Object elementData[ ] The array that holds the vector is stored in elementData.

Apart from the methods defined by Collection and List, Vector has defined several legacy method of its own. These legacy methods are as defined below:

Method Name Description
void addElement(Object element) This will add the object specified by element to the vector invoking this method.
int capacity( ) This will return the capacity of vector.
Object clone( ) This will create replica of vector invoking this method
boolean contains(Object element) This will true if vector invoking this method contains element or else false.
void copyInto(Object array[ ]) The element of vector invoking this method will be copied into the array.
Object elementAt(int index) This will fetch the element present at the specified index.
Enumeration elements( ) This will return an enumeration of elements of vector invoking this method.
void ensureCapacity(int size) This is to ensure minimum capacity of vector invoking this method to size.
Object firstElement( ) This will return the first element of the vector invoking this method.
int indexOf(Object element) This will return the index of element from the vector invoking this method.
int indexOf(Object element, int start) This will return the index of the first occurrence of element at or after start. If the object is not in that portion of the vector, –1 is returned.
void insertElementAt(Object element, int index) This will insert Object element at specified index position index.
boolean isEmpty( ) This will return false if vector invoking this method is empty else true.
Object lastElement( ) This will fetch the last element of the vector invoking this method.
int lastIndexOf(Object element) This will returns the index of the last occurrence of element. If the object is not in the vector, –1 is returned.
int lastIndexOf(Object element, int start) This will returns the index of the last occurrence of element before start. If the object is not in that portion of the vector, –1 is returned
void removeAllElements( ) This will empties the vector. After this method executes, the size of the vector is zero
boolean removeElement(Object element) This will remove element from the vector. If more than one instance of the specified object exists in the vector, then it is the first one that is removed. Returns true if successful and false if the object is not found.
void removeElementAt(int index) This will remove the element present at specified index position index.
void setElementAt(Object element, int index) The index location specified is assigned the element.
void setSize(int size) This will sets the number of elements in the vector to size. If the new size is less than the old size, elements are lost. If the new size is larger than the old size, null elements are added.
int size( ) This will return the size of vector invoking this method.
String toString( ) This will return the string equivalent of vector invoking this method.
void trimToSize( ) This will set the vector’s capacity equal to the number of elements that it currently holds.

Let see an example to understand how size of vector grows:

OUTPUT:

Initial vector capacity 10
vector capacity after adding 7 elements 10
vector capacity after adding total 12 elements 20
Element at given index=2 is 3
Size of given vector is: 12
Is given vector empty? false
Is given vector empty? true

In above example we can see that initial size of vector is 10 but as number of elements added exceeds the initial capacity, the increases to double the initial size.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.