Vector Class in Java

Vector Class

The Vector class is similar to ArrayList. But Vector class is synchronized. It is used to implement the dynamic array. Vector class extends the AbstractList class and implements List interface.

Example : Program to implement methods available in Vector class

import java.util.*;
public class VectorDemo
{
     public static void main(String args[])
     {
          //creates vector
          Vector<Integer> vec = new Vector<Integer>();
          System.out.println("Initial size of Vector: "+vec.size());
          System.out.println("Initial capacity of Vector: "+vec.capacity());

          //adding the elements
          for(int i = 0; i <= 10; i++)
          {
               vec.add(new Integer(i * 5));
          }

          //retrving the element
          System.out.println("Vector element after adding: "+vec);
          System.out.println("Size of the Vector after adding: "+vec.size());
          System.out.println("Capacity of Vector after adding: "+vec.capacity());

          //delete the elemnt from the list
          vec.removeElement(0);
          vec.remove(5);
          System.out.println("Vector elements after deleting: "+vec);
          System.out.println("Size of the Vector after deleting: "+vec.size());
          System.out.println("Capacity of Vector after deleting: "+vec.capacity());
     }
}

                          
Output:
Initial size of Vector: 0
Initial capacity of Vector: 10
Vector element after adding: [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
Size of the Vector after adding: 11
Capacity of Vector after adding: 20
Vector elements after deleting: [5, 10, 15, 20, 25, 35, 40, 45, 50]
Size of the Vector after deleting: 9
Capacity of Vector after deleting: 20