Vector Tutorial In Java With Example

Vector is type of data structure that implements List Interface. It is very much similar to ArrayList as it also maintains insertion order, that is elements are retrieved in same order as they are added into it.

Like Arrays it has index value that is automatically allocated to it, but main think which distinguishes it is, it can grow or shrink its capacity according to the need after it is created, as it implements growable array.

Important Note: Vector contains many legacy methods that are not part of collection framework which we will discuss below.


Example of Vector Class:

Let us discuss Vector Class with the help of program. In this program we will create an object of Vector Class, some integer values are added and displayed.

Following program has been divided into 3 Steps that we will discuss one by one:

import java.util.*;
public class VectorDemo
{

  public static void main(String args[]){
//Step 1: Vector object is created
  Vector<Integer> vectorObject = new Vector<Integer>(4); 
 
//Step 2: Integers are added
  vectorObject.add(3);  
  vectorObject.add(5);
  vectorObject.add(4);
  vectorObject.add(1);
  vectorObject.add(2);
  vectorObject.add(8);
  vectorObject.add(8);

//Step 3: Values are Displayed
  System.out.println("Values in Vector Object  :" +vectorObject); 
  }
}

Output:

Values in Vector Object :[3, 5, 4, 1, 2, 8, 8]

Explanation of Example:

  • In Step 1, we have created object of Vector class, we have defined these objects to store values of Integer type.
  • In Step 2, we have used add method to store values in the data structure that we have created in step 1.
  • In Step 3, we have displayed values of these object.

Vector Methods In Java:

 1. boolean add(E e)

This method adds element at the end of the vector, as shown in the following program:

import java.util.*;

public class VectorMethodsDemo
{

  public static void main(String args[]){

  Vector<Integer> vectorObject = new Vector<Integer>(4);

  vectorObject.add(3);

  vectorObject.add(5);

  vectorObject.add(4);

  vectorObject.add(1);

  vectorObject.add(2);

  vectorObject.add(8);

  vectorObject.add(8);

  System.out.println("Values in Vector Object  :" +vectorObject);

  }

}

Output:

Values in Vector Object  :[3, 5, 4, 1, 2, 8, 8]

2. void add(int index, E element)

This method adds an element at specified index and moves the whole elements one step in forward direction as shown in the following program:

import java.util.*;

public class VectorMethodsDemo
{

  public static void main(String args[]){

  Vector<Integer> vectorObject = new Vector<Integer>(4);

  vectorObject.add(0,3);

  vectorObject.add(1,5);

  vectorObject.add(2,4);

  vectorObject.add(3,1);

  for(Integer integer: vectorObject)
  {

 System.out.println("Index : " +vectorObject.indexOf(integer)+ " Value:  "+integer);

  }

//Adding element at index 2

  vectorObject.add(2, 10);

  System.out.println("\nAfter adding value at index 2:\n");

  for(Integer integer: vectorObject)

  {   
 System.out.println("Index : " +vectorObject.indexOf(integer)+ " Value: " +integer);

  }
 }
}

Output:

Index : 0 Value: 3

Index : 1 Value: 5

Index : 2 Value: 4

Index : 3 Value: 1

After adding value at index 2:

Index : 0 Value: 3

Index : 1 Value: 5

Index : 2 Value: 10

Index : 3 Value: 4

Index : 4 Value: 1

 3. void addElement(E obj)

This method adds the specified element at the end of the vector also increasing its size by 1, as shown in the following program:

import java.util.*;

public class VectorMethodsDemo
{

  public static void main(String args[]){

  Vector<Integer> vectorObject = new Vector<Integer>(4);

  vectorObject.add(3);

  vectorObject.add(5);

  vectorObject.add(4);

  vectorObject.add(1);

  System.out.println("Values in Vecor object" +vectorObject);

  vectorObject.addElement(2);

  System.out.println("Values in Vecor object" +vectorObject);

  }

}

Output:

Values in Vecor object[3, 5, 4, 1]

Values in Vecor object[3, 5, 4, 1, 2]

4. int capacity()

This method gives the capacity of the vector, as shown in the following program:

import java.util.*;

public class VectorMethodsDemo
{

  public static void main(String args[]){

  Vector<Integer> vectorObject = new Vector<Integer>(4);

  vectorObject.add(3);

  vectorObject.add(5);

  vectorObject.add(4);

  vectorObject.add(1);

  System.out.println("current capacity of Vector object is " +vectorObject.capacity());

  }

}

Output:

current capacity of Vector object is 4

Read All Vector Methods In JAVA With Example


Important Points Of Vector:

  • Vector is very much similar to ArrayList, but with few differences, firstly Vector is thread safe, where as Arraylist is not. Secondly, Vector is synchronized, where as Arraylist is not, So performance of Vector is low than ArrayList.
  • Vector increases its size by 100 % that is it doubles its size when total number of elements exceeds its capacity whereas ArrayList increases by 50 % of current array size.
  • Vector can use Enumeration interface as well as Iterator to traverse over its elements.
  • Vector is a legacy Class, that is it is introduced before JDK 1.2.

Importance of Vectors:

  • As vectors implements growable array. It grows and shrinks according to the need. So it is very helpful when we don’t know requirement in advance, we can just declare a vector with some initial capacity, that automatically doubles its size when elements are added or removed.
  • Vectors are thread safe, so it can be used in Multithreaded Environment.

Quick Revision Points Of Vector:

  • Vector doubles its size when elements exceeds its initial capacity.
  • Vector can use both Enumeration as well as Iterator to traverse elements.
  • Vector is synchronized.
  • Vector is Thread Safe.
  • Vectors can be used for Multi Threading.
  • Vector is slow as compared to Array List.

Ask Your Question Or Leave A Reply

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