Vector Class

Vector Class Java with Programming Example

Vector ClassIn this chapter you will learn:

  1. What is Vector Class in Java?
  2. Difference between ArrayList and Vector
  3. Initialization of Vector Cass
  4. Methods of Vector Class in Java
  5. Programming Example
 What is Vector Class in Java?

Vector is like an array but the difference is that it can grow and shrink dynamically and doubly. Like an array the element of vector can also be accessed through its index position. It optimizes storage management by maintaining capacity and capacityIncrement. It means if you remove an element the size of vector automatically decreases. Vector Class has synchronized behavior so it gives weak performance in searching, adding, delete and update of its elements.

How to Initialize Vector?

There are three way to initialize vector.

1. Vector V=new Vector();

It is the default way to create vector. It creates an empty vector with the size of 10. The size of vector will grow when it needs 11th items.

2. Vector object= new Vector(int initialCapacity)
Vector vc=new Vector(5);

It will create vector with initial size 5.

3. Vector object= new vector(int initialcapacity, capacityIncrement)
Vector vec= new Vector(4, 6)

It will create vector with size 4 and increment capacity is 6. It means when you add 5th element in the list the size of vector will be increased by 6 and capacity of vector will be 10.

Different Between ArrayList and Vector

ArrayListVector
It is not SynchronizedIt is Synchronized
It increments 50% of current array size if number of elements exceeds from its capacity.It increments 100% of current array size. It means it can grow double of ArrayList.
It is not a legacy class.It is legacy class.
It gives faster performance on searching, adding, delete and update because it is not synchronized.It gives poor performance because it is synchronized.
It uses Iterator interface to traverse the element.It uses Enumeration interface to traverse the element. But it can use Iterator also.

Constructors of Vector class

ConstructorsDescriptions
Vector()Constructs an empty vector so that its internal data array has size 10 and its standard capacity increment is zero.
Vector(Collection<? extends E> c)Constructs a vector containing the elements of the specified collection, in the order they are returned by the collection’s iterator.
Vector(int initialCapacity)Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero.
Vector(int initialCapacity, int capacityIncrement)Constructs an empty vector with the specified initial capacity and capacity increment.

Methods of Vector Class

MethodsDescriptions
boolean add(E e)Append the element at the end of the list.
void add(int index, E element)Insert the element at the specified position in the list.
boolean addAll(
Collection<? extends E> c)
Append entire element from one collection to the end of the list. The orders are defined as the iterator returns value.
boolean addAll(
int index, Collection<? extends E> c)
Insert entire elements from one collection to list at the specified postion.
void addElement(E obj)Adds the specified component to the end of this vector, increasing its size by one.
int capacity()Returns the current capacity of this vector.
void clear()Clear the list by removing all the items.
Object clone()Make a clone of arraylist.
boolean contains(Object o)returns true if arraylist contains specified items.
boolean containsAll(Collection<?> c)Returns true if this Vector contains all of the elements in the specified Collection.
void copyInto(Object[] anArray)Copies the components of this vector into the specified array.
E elementAt(int index)Returns the component at the specified index.
Enumeration elements()Returns an enumeration of the components of this vector.
void ensureCapacity(int minCapacity)Increase the capacity of arraylist.
boolean equals(Object o)Compares the specified Object with this Vector for equality.
E firstElement()Returns the first component (the item at index 0) of this vector.
E get(int index)Returns the element at the specified index position in the list.
int hashCode()Returns the hash code value for this Vector.
int indexOf(Object o)Returns the index of first occurrence of element in the list. If item not found returns -1.
int indexOf(Object o, int index)Returns the index of the first occurrence of the specified element in this vector, searching forwards from index, or returns -1 if the element is not found.
void insertElementAt(E obj, int index)Inserts the specified object as a component in this vector at the specified index.
boolean isEmpty()Returns true if list is empty.
Iterator iterator()Returns an iterator over the elements in this list in proper sequence.
E lastElement()Returns the last component of the vector.
int lastIndexOf(Object o)Returns the index of last occurrence of element in the list. If item not found returns -1.
int lastIndexOf(Object o, int index)Returns the index of the last occurrence of the specified element in this vector, searching backwards from index, or returns -1 if the element is not found.
ListIterator<E> listIterator()Returns a list iterator over the elements in this list (in proper sequence).
ListIterator<E> listIterator(int index)Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
E remove(int index)Removes the element at the specified position in this list.
boolean remove(Object o)Removes the first occurrence of the specified element from this list, if it is present.
boolean removeAll(Collection<?> c)Removes from this list all of its elements that are contained in the specified collection.
void removeAllElements()Removes all components from this vector and sets its size to zero.
boolean removeElement(Object obj)Removes the first (lowest-indexed) occurrence of the argument from this vector.
void removeElementAt(int index)Deletes the component at the specified index.
protected void removeRange(
int fromIndex, int toIndex)
Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
boolean retainAll(Collection<?> c)Retains only the elements in this list that are contained in the specified collection.
E set(int index, E element)Replaces the element at the specified position in this list with the specified element.
void setElementAt(E obj, int index)Sets the component at the specified index of this vector to be the specified object.
void setSize(int newSize)Sets the size of this vector.
int size()Returns the number of elements in this list.
List subList(int fromIndex, int toIndex)Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
Object[] toArray()Returns an array containing all of the elements in this list in proper sequence (from first to last element).
<T> T[] toArray(T[] a)Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
String toString()Returns a string representation of this Vector, containing the String representation of each element.
void trimToSize()Trims the capacity of this ArrayList instance to be the list’s current size.

 

Programming Example

Output

D:\JavaProgram>javac Vector_Class.java

D:\JavaProgram>java Vector_Class
[Laptop, Desktop, iPhone]
Vector Size is 3
Default Capacity Increment is 4
If Contains iPhone : true
First Element is : Laptop
Last Element is : iPhone
Index Position of Desktop : 1
Is Vector Empty ? : false
New List is after adding Mobile : [Laptop, Desktop, Mobile, iPhone]
New List is after removing Desktop : [Laptop, Mobile, iPhone]
Elements are :
Laptop , Mobile , iPhone ,
Elements are :
Laptop , Mobile , iPhone ,
_

Summary

In this chapter you have learned all about Vector class in Java. Vector is like an array list but the difference is that is synchronized and exceeds its capacity size doubly. In the next chapter you will learn Stack Class in Java.


Leave a Reply

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