Java Vector Class

The Vector class is a legacy class that implements a growable array of objects. The components that it contains can be accessed using an integer index. The size of a Vector can grow or shrink as required to add and remove items after the Vector created.

Vectors are similar to ArrayList, but Vector is synchronized. It implements the List interface and extends AbstractList.

Vector tries to optimize storage by maintaining a capacity and capacityIncrement. Always, the capacity at least as large as the size of the vector. To reduce the amount of incremental reallocation, the methods usually increases the capacity of a vector before inserting a large number of elements.

Java Vector Fields:

capacityIncrement It increases the capacity of the vector automatically when its size becomes greater than its capacity.
elementCount It returns the valid components in this vector object.
elementData The components of the vector are stored in this array buffer.

Constructors of Vector and its usages:

Vector() It constructs an empty vector with size 10 and with zero standard capacity increment.
Vector(Collection c) It creates a vector with elements of the specified collection in the arguments; in the order, they are returned by the collection’s iterator.
Vector(int initialCapacity) It constructs an empty vector with specified initial capacity in the argument, with capacity increment equals to zero.
Vector(int initialCapacity, int capacityIncrement) It constructs an empty vector with specified initial capacity and capacity increment.

Methods of Java Vector and their usages:

Modifier and Data type Methods Description
Boolean add(E e) It adds the specific element to the end of the Vector
addAll(Collection c) It will add all the elements in the specified collection to the end of the Vector, in the same order as it is in the specified collection.
addAll(int index, Collection c) It adds all the elements in the vector from the specified collection at the specified index position in the arguments.
contains (Object o) It will return true if the specified element present in the vector
containsAll(Collection c) Will return true if all the elements in the specified collection are present in the vector.
equals(Object o) It will compare the specified object with the vector for equality.
isEmpty() Used to test if the vector has no components.
remove(Object o) It will delete the first occurrence of the specified element in the vector. In case the element is not present, then status will be unchanged.
removeAll(Collection c) Removes all the Vector elements that are present in the specified collection.
removeElement(Object obj) It will remove the first occurrence (from lowest index) of the argument from the vector
retainAll(Collection c) It will keep only the elements in the vector that are contained in the specified Collection.
int capacity() It will return the current capacity of the vector.
hashCode() It returns the hash value of the vector.
indexOf(Object o) It will return the index of the first occurrence of the element specified in the argument or returns –ve one if the element is not present in the vector.
indexOf(Object o, int index) It will return the index of the first occurrence of the element specified in the argument, searching forward from index or return -1 if the element is not present in the vector.
lastIndexOf(Object o) It will return the index of the last occurrence of the element specified in the argument or -1 if the element is not in the vector.
lastIndexOf(Object o, int index) It also returns the last occurrence of the element specified in the argument but searching backward from the index or it returns -1 if the vector does not contain the specified element.
Size( ) Used to get the number of elements present in the vector.
void add(int index, E element) It will insert the specified element at the specified position in the vector.
addElement(E obj) It appends the specified component at the end of the vector, increasing its size by 1.
clear() Used to remove the elements from the vector.
copyInto(object[] anArray) It will copy the elements of the vector to the specified array.
ensureCapacity(int minCapacity) Used to increase the capacity of the vector if necessary, to ensure that it holds the number of elements specified by the minCapacity argument.
insertElementAt(E obj, int index) Used to insert the specified object as an element in the vector at the specified index position.
removeAllElements() Removes all elements from the vector and set the size to zero.
removeElementAt(int index) It deletes the element at the specified index location.
removeRange(int fromIndex, int toIndex) Used to remove all the element from the list whose index is between fromIndex(inclusive) and toIndex(exclusive).
setElementAt(E obj, int index) Used to set the element at the specified index of this vector to be the specified object.
setSize(int newSize) It sets the size of the vector to the specified argument.
trimToSize() It will trim the capacity of the vector to be the vectors current size.
Object clone() Used to clone the vector/exact copy of the vector
toArray() It always returns an array containing all the values in the vector in the sequence.
String toString() It returns an array that contains all the elements in the vector in the correct order.
E elementAt(int index) It returns the elements at the specified index.
firstElement() It returns the first element of the vector (item at index 0).
get(int index) It will return the values at the specified position in the vector.
lastElement() It will return the last element of the vector.
remove(int index) It is used to remove the element from the specified position in the vector.
set(int index, E element) It will replace the element at the specified position in the vector with specified elements in the argument.
Enumeration elements() It will return an enumeration of the elements of this vector.
Iterator iterator() Always returns an iterator over the elements in the list in proper sequence.
ListIterator listIterator() It always returns a ListIterator over the values in the list (in sequence).
ListIterator listIterator(int index) It always returns a list iterator over the elements in the list, starting at the specified position
List sublist(int fromIndex, int toIndex) It will return a view of the area of the list between the starting and ending specified index position.
T[] toArray(T[] a) It returns an array that contains all the elements of the vector in the sequential order, and the runtime type of the returned array is that of the specified array.

Example to illustrate add() method.

Output:

Example to illustrate addAll(Collection c) method.

Output:

Example to Demonstrate addAll(int index, Collection c) method.

Output:

Example to illustrate contains() method.

Output:

Example to illustrate containsAll() method.

Output:

Example to illustrate equals() method

Output:

Example to illustrate isEmpty() method.

Output:

Example to illustrate remove() method.

Output:

Example to illustrate removeElement() method.

Output:

Example to illustrate retainAll() method.

Output:

Example to illustrate capacity() method.

Output:

Example to illustrate hashCode() method.

Output:

Example to illustrate indexOf() method.

Output:

Example to illustrate lastIndexof() method.

Output:

Example to illustrate size() method.

Output:

Example to illustrate add(int index, Object obj) method.

Output:

Example to illustrate addElement(E obj)

Output:

Example to Illustrate void clear() method.

Output:

Example to illustrate copyInto() method.

Output:

Example to illustrate ensureCapacity() method.

Output:

Example to illustrate insertElementAt() method.

Output:

Example to illustrate removeAllElements() method.

Output:

Example to illustrate setElementAt() method

Output:

Example to illustrate setSize() method.

Output:

Example to illustrate trimToSize() method.

Output:

Example to illustrate clone() method.

Output:

Example to illustrate toArray() method.

Output:

Example to illustrate toString() method.

Output:

Example to illustrate elementAt(int Index) method.

Output:

Example to illustrate firstElement() method.

Output:

Example to illustrate get() methods

Output:

Example to illustrate lastElement() method

Output:

Example to illustrate remove(int Index) method. 

Output:

Example to illustrate set() method.

Output:

Example to illustrate the elements() method .

Output:

Example to illustrate iterator()

Output:

Example to illustrate listIterator() method.

Output:

Example to illustrate listIterator(int index) method.

Output:

Example to illustrate subList(int fromIndex, int toIndex) method

Output:

Example to illustrate toArray() method.

Output:

Pin It on Pinterest

Share This