AppDividend
Latest Code Tutorials

Java Vector Class Tutorial With Example From Scratch

0

Java Vector Class Tutorial With Example From Scratch is today’s topic. The Java Vector class is a part of the Java Collection framework. It is used to create arrays whose size can increase or decrease (that is, Dynamic Array) during the execution. The Java Vector class implements the List interface in Java Collection framework. The main difference between the Vector class and other Collection classes is that the Vector class is synchronous, unlike others.

Java Vector Class

Vector class contains Legacy methods, unlike other Collection classes. Initially, Vector class was a Legacy class. It was added to the Collection framework later. Collection framework was not included in the early version of java. So, Legacy classes are used to store objects.

The hierarchy of the Vector class is shown in the diagram below.

Java Vector Class

#Constructors

  1. Vector(): It creates a Vector with default capacity 10. Capacity tells the number of elements that can be added to the Vector without using dynamic memory allocation.
  2. Vector(Collection c): Creates a Vector with the elements of given Collection.
  3. Vector(int cap): It creates a Vector with capacity defined by the cap.
  4. Vector(int cap, int inc): It creates a Vector with a cap as the initial capacity and inc as increment value. The increment value tells how much capacity needs to be allocated every time when the size of the Vector increases. If this value is not specified, the capacity of the Vector will get doubled every time allocation takes place.

#Vector Class Methods

#boolean add(Object o):

It inserts an object to the end of the Vector. It returns true if insertion is successful.

Syntax : public boolean add(Object o)

Program

import java.util.Vector;
class E3
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();
    v.insertElementAt(10,0);
    v.insertElementAt(20,0);
    v.insertElementAt(30,0);
    System.out.println(v);
  }
}

Output

boolean add(Object o)

#void add(int i, Object o):

It inserts the object at index i, resulting in the shifting of elements to the right from index i. It throws an IndexOutOfBoundsException if the specified index is greater than the size of the Vector.

Syntax: public void add(int i, Object o)

Program

import java.util.Vector;
import java.util.Vector;
class E2
{
  public static void main(String[] args)
  {
    Vector v=new Vector<>();
    v.add("mercury");
    v.add("earth");
    v.add(1,"venus"); //adds the object to the index 1
    System.out.println(v);
  }
}

Output

void add(int i, Object o)

#void insertElementAt(Object o, int i):

It inserts the item at the given index. It is similar to the add(int I, Object o) method.

Syntax : public void insertElementAt(Object o, int i)

Program

import java.util.Vector;
class E3
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();
    v.insertElementAt(10,0);
    v.insertElementAt(20,0);
    v.insertElementAt(30,0);
    System.out.println(v);
  }
}

Output

 

void insertElementAt(Object o, int i)

#boolean addAll(Collection c)

It inserts all the elements of the collection to the end of the Vector. It throws NullPointerException in the case when the Collection is null.

Syntax : public boolean addAll(Collection c)

 

boolean addAll(Collection c)

#boolean addAll(int i, Collection c)

It insert all the elements of the collection at a particular index. It throws NullPointerException and IndexOutOfBoundsException.

Syntax : public boolean addAll(int i, Collection c)

Program

import java.util.Vector;
import java.util.ArrayList;
class E5
{
  public static void main(String[] args)
  {
    ArrayList<Integer> arrList=new ArrayList<>();
    arrList.add(5);
    arrList.add(10);
    arrList.add(15);
    Vector<Integer> v=new Vector<>();
    v.add(0);
    v.add(20);
    v.addAll(1,arrList); //adds the whole arraylist to the vector at index 1
    System.out.println(v);
  }
}

Output

 

boolean addAll(int i, Collection c)

#void setElementAt(Object o, int i)

It replaces the element present at index i with the specified object. If the index is greater than the size of the vector, ArrayIndexOutOfBoundsException is thrown.

Syntax : public void setElementAt(Object o, int i)

Program

import java.util.Vector;
class E6
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();
    v.add(10);
    v.add(20);
    v.add(32);
    System.out.println(v);
    v.setElementAt(30,2);
    System.out.println(v);
  }
}

Output

 

void setElementAt(Object o, int i)

#Object get(int i)

It returns the element present on the ith index. It throws IndexOutOfBoundsException if the specified index exceeds the size of the Vector.

Syntax : public  synchronized Object get(int i)

Program

import java.util.Vector;
class E7
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();
    v.add(10);
    v.add(20);
    v.add(30);
    for(int i=0;i<3;i++)
      System.out.println("value at index: "+i+"  "+v.get(i));
  }
}

Output

 

Object get(int i)

#Object firstElement():

It returns the element present at index 0 of the Vector. If the Vector is empty, it throws NoSuchElementException.

Syntax : public Object firstElement( )

import java.util.Vector;
class E8
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();
    v.add(10);
    v.add(20);
    v.add(30);
    int i=v.firstElement();
    System.out.println("first element : "+i);
  }
}

Output

 

Object firstElement()

#Object lastElement():

It returns the last present element in the Vector, that is, element on index size-1. 

Syntax : public Object lastElement( )

Program

import java.util.Vector;
class E9
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();
    v.add(10);
    v.add(20);
    v.add(30);
    int i=v.lastElement();
    System.out.println("last element : "+i);
  }
}

Output

 

Object lastElement()

#void clear():

It deletes all the elements of the Vector.

Syntax : public void clear( )

Program

import java.util.Vector;
class E10
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();
    v.add(10);
    v.add(20);
    v.add(30);
    System.out.println(v);
    v.clear();
    System.out.println(v);
  }
}

Output

 

void clear()

#void removeAllElements()

It deletes all the elements of the Vector. This method is identical to the method clear().

Syntax : public void removeAllElements( )

Program

import java.util.Vector;
class E11
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();
    v.add(10);
    v.add(20);
    v.add(30);
    System.out.println(v);
    v.removeAllElements();
    System.out.println(v);
  }
}

Output

void removeAllElements()

 

#boolean remove(Object o)

It deletes the first occurrence of the specified element from the Vector. Returns true on successful deletion. Syntax : public boolean remove(Object o)

Program

import java.util.Vector;
class E12
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();
    v.add(10);
    v.add(20);
    v.add(30);
    System.out.println(v);
    Integer i=v.get(1);
    v.remove(i);
    System.out.println(v);
  }
}

Output

 

boolean remove(Object o)

#boolean removeElement(Object o)

It deletes the first occurrence of the specified element. It is Similar to the remove method.

Program

import java.util.Vector;
class E13
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();
    v.add(10);
    v.add(20);
    v.add(30);
    System.out.println(v);
    Integer i=v.get(1);
    v.removeElement(i);
    System.out.println(v);
  }
}

Output

 

boolean removeElement(Object o)

#boolean removeAll(Collection c)

It deletes all the elements from the Vector which are contained in the specified Collection.

Syntax : public  synchronized boolean removeAll(Collection c)

Program

import java.util.Vector;
import java.util.ArrayList;
class E14
{
  public static void main(String[] args)
  {
    ArrayList<String> arrList=new ArrayList<>();
    arrList.add("apple");
    arrList.add("mango");
    arrList.add("banana");
    Vector<String> v=new Vector<>();
    v.add("apple");
    v.add("mango");
    v.add("orange");
    v.add("grapes");
    System.out.println(v);
    v.removeAll(arrList);
    System.out.println("after removing: "+v);
  }
}

Output

 

boolean removeAll(Collection c)

#Object clone()

It returns a copy of the Vector.

Syntax : public Object clone( )

Program

import java.util.Vector;
class E15
{
  public static void main(String[] args)
  {
    Vector<String> v=new Vector<>();
    v.add("apple");
    v.add("mango");
    v.add("orange");
    v.add("grapes");

    Vector<String> duplicate=(Vector)v.clone();

    System.out.println(duplicate);
  }
}

Output

 

Object clone()

#boolean contains(Object o)

It returns true if the specified item is present in the Vector.

Syntax : public boolean contains(Object o)

Program

import java.util.Vector;
class E16
{
  public static void main(String[] args)
  {
    Vector<String> v=new Vector<>();
    v.add("apple");
    v.add("mango");
    v.add("orange");
    v.add("grapes");

    boolean x=v.contains("apple");
    if(x)
      System.out.println("vector contains apple");
    else
      System.out.println("vector does not contains apple");
  }
}

Output

 

boolean contains(Object o)

#boolean containsAll(Collection c)

It returns true if all the items of the specified Collection are present in the Vector.

Syntax : public synchronized boolean containsAll( Collection c)

Program

import java.util.Vector;
import java.util.ArrayList;
class E17
{
  public static void main(String[] args)
  {
    ArrayList<String> arrList=new ArrayList<>();
    arrList.add("apple");
    arrList.add("mango");
    arrList.add("banana");
    System.out.println(arrList);
    Vector<String> v=new Vector<>();
    v.add("apple");
    v.add("mango");
    v.add("orange");
    v.add("grapes");
    System.out.println(v);

    if(v.containsAll(arrList))
      System.out.println("vector contains whole array list");
    else
      System.out.println("vector does not contain whole array list"); 
  }
}

Output

 

boolean containsAll(Collection c)

#void ensureCapacity(int cap)

It increases the current capacity of the Vector to the cap.

Syntax : public void ensureCapacity(int cap )

Program

import java.util.Vector;
class E18
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();
    System.out.println("initial capacity : "+v.capacity());
    v.ensureCapacity(50);
    System.out.println("new capacity : "+v.capacity());
  }
}

Output

 

void ensureCapacity(int cap)

#int indexOf(Object o)

It returns the index where the specified element first occurs in the Vector. The methods return -1 if the element is not present.

Syntax : public int indexOf(Object o)

Program

import java.util.Vector;
class E19
{
  public static void main(String[] args)
  {
    Vector<String> v=new Vector<>();
    v.add("rat");
    v.add("cat");
    v.add("bat");
    v.add("dog");
    v.add("bat");
    v.add("cat");
    v.add("bat");
    int i=v.indexOf("bat");
    System.out.println("first index of bat : "+i);
  }
}

Output

 

int indexOf(Object o)

#int indexOf(Object o, int i)

It returns an index of the first occurrence of the specified element after index i.

Syntax: public synchronized int indexOf(Object o, int i)

Program

import java.util.Vector;
class E20
{
  public static void main(String[] args)
  {
    Vector<String> v=new Vector<>();
    v.add("rat");
    v.add("cat");
    v.add("bat");
    v.add("dog");
    v.add("bat");
    v.add("cat");
    v.add("bat");
    int i=v.indexOf("bat",3);  //first index of bat after 3rd index
    System.out.println("first index of bat after 3rd index: "+i);
  }
}

Output

int indexOf(Object o, int i)

 

#int lastIndexOf(Object o, int i)

It returns the index of the last occurrence of the element int the Vector before index i.

Syntax: public int lastIndexOf(Object o, int i)

Program

import java.util.Vector;
class E22
{
  public static void main(String[] args)
  {
    Vector<String> v=new Vector<>();
    v.add("rat");
    v.add("cat");
    v.add("bat");
    v.add("dog");
    v.add("bat");
    v.add("cat");
    v.add("bat");
    int i=v.lastIndexOf("cat",4);  //last index of cat before 4th index
    System.out.println("last index of cat before 4th index: "+i);
  }
}

Output

 

int lastIndexOf(Object o, int i)

#boolean isEmpty()

It returns true if the Vector does not contain any element, false otherwise.

Syntax : public boolean isEmpty( )

Program

import java.util.Vector;
class E23
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();
    if(v.isEmpty()==true)
      System.out.println("vector is empty");
    else
      System.out.println("vector is not empty");
  }
}

Output

 

boolean isEmpty()

#boolean equals(Object o)

It compares the Vector and the specified object and returns true if both are equal.

Syntax : public boolean equals(Object o)

Program

import java.util.Vector;
class E24
{
  public static void main(String[] args)
  {
    Vector<Integer> v1=new Vector<>();
    v1.add(10);
    v1.add(20);
    v1.add(30);
    Vector<Integer> v2=new Vector<>();
    v2.add(30);
    v2.add(20);
    v2.add(10);
    if(v1.equals(v2))
      System.out.println("both vectors are equal");
    else
      System.out.println("both vectors are not equal");
  }
}

Output

 

boolean equals(Object o)

#void trimToSize()

It reduces the capacity of the Vector to its size, that is, to the current number of elements present in Vector.

Syntax : public void trimToSize( )

Program

import java.util.Vector;
class E25
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();

    v.add(10);
    v.add(20);
    v.add(30);

    System.out.println("size of the vector: "+v.size());
    System.out.println("capacity before trim: "+v.capacity());

    v.trimToSize();
    System.out.println("capacity after trim: "+v.capacity());
  }
}

Output

 

void trimToSize()

#String toString()

It returns the Vector as a String.

Syntax : public String toString( )

Program

import java.util.Vector;
class E26
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();
    v.add(10);
    v.add(20);
    v.add(30);
    String str=v.toString();
    System.out.println(str);
  }
}

Output

 

String toString()

#Object[ ] toArray( )

It returns the array containing the elements of the Vector.

Syntax : public Object[ ]  toArray( )

Program

import java.util.Vector;
class E27
{
  public static void main(String[] args)
  {
    Vector<String> v=new Vector<>();
    v.add("delhi");
    v.add("mumbai");
    v.add("hyderabad");

    String[] arr= new String[3];
    arr=(String[])v.toArray(arr);

    for(int i=0;i<3;i++)
      System.out.println(arr[i]);
  }
}

Output

 

Object[ ] toArray( )

#void copyInto(Object[ ] arr)

It copies all the elements of the vector to the specified array.

Syntax: public void copyInto(Object[ ] arr

Program

import java.util.Vector;
class E28
{
  public static void main(String[] args)
  {
    Vector<String> v=new Vector<>();
    v.add("delhi");
    v.add("mumbai");
    v.add("hyderabad");

    String[] arr= new String[3];
    v.copyInto(arr);

    for(int i=0;i<3;i++)
      System.out.println(arr[i]);
  }
}

Output

 

void copyInto(Object[ ] arr)

#int size()

It returns the total number of elements present in the Vector.

Syntax : public int size( )

Program

import java.util.Vector;
class E29
{
  public static void main(String[] args)
  {
    Vector<String> v=new Vector<>();
    v.add("delhi");
    v.add("mumbai");
    v.add("hyderabad");

    int len=v.size();
    System.out.println("size of the vector: "+len);
  }
}

Output

 

int size()

#int capacity()

It returns the current capacity of the Vector.

Syntax : public int capacity( )

Program

import java.util.Vector;
class E30
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();
    System.out.println("initial capacity of the vector: "+v.capacity());

    for(int i=0;i<15;i++)
      v.add(i);

    //new capacity doubles itself 
    System.out.println("new capacity of the vector: "+v.capacity());
  }
}

Output

 

int capacity()

#void setSize(int s)

Changes the size of the Vector. If the new size is less than the current size, then elements from index s will be deleted. If the new size is greater than a current size, then null objects are added after the last element. It throws ArrayIndexOutOfBoundsException if the negative size is specified.

Syntax : public void setSize(int s)

Program

import java.util.Vector;
class E31
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();
    for(int i=0;i<15;i++)
      v.add(i);

    System.out.println("vector: "+v);
    System.out.println("size of vector: "+v.size());

    v.setSize(5);

    System.out.println("new vector: "+v);
    System.out.println("new size of vector: "+v.size());
  }
}

Output

 

void setSize(int s)

#boolean retainAll(Collection c)

It deletes all the elements from the Vector which are not present in the specified collection. In case the Collection is null, it throws NullPointerException.

Syntax : public boolean retainAll(Collection c)

Program

import java.util.Vector;
import java.util.ArrayList;
class E32
{
  public static void main(String[] args)
  {
    ArrayList<String> arrList=new ArrayList<>();
    arrList.add("apple");
    arrList.add("mango");
    arrList.add("banana");
    System.out.println(arrList);
    Vector<String> v=new Vector<>();
    v.add("apple");
    v.add("mango");
    v.add("orange");
    v.add("grapes");
    System.out.println(v);

    v.retainAll(arrList);
    System.out.println("new vector is: "+v);

  }
}

Output

boolean retainAll(Collection c)

 

#int hashCode()

It returns the hashCode of the Vector.

Syntax : public int hashCode( )

Program

import java.util.Vector;
class E33
{
  public static void main(String[] args)
  {
    Vector<Integer> v=new Vector<>();
    v.add(10);
    v.add(20);
    v.add(30);
    int h_code=v.hashCode();
    System.out.println("hash code of vector: "+h_code);
  }
}

Output

 

int hashCode()

Finally, Java Vector Class Tutorial With Example From Scratch is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.