Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe for all free courses

How to use Vector in Java with example ?.

Program to demonstrate how to implement Vector class in java.util.*; package along with its operations :

package com.hubberspot.collections.example;

import java.util.*;

public class VectorExample 
{
  public static void main(String args[]) 
  {
    Vector vector = new Vector(3, 2);
      
    System.out.println("Initial size: " + vector.size());
    System.out.println("Initial capacity: " +
      
    vector.capacity());
      
    vector.addElement(new Integer(3));
    vector.addElement(new Integer(4));
    vector.addElement(new Integer(5));
    vector.addElement(new Integer(6));
      
    System.out.println("Capacity after adding 4 elements: " +
          vector.capacity());

    vector.addElement(new Double(6.35));
      
    System.out.println("Current capacity: " +
          vector.capacity());
      
    vector.addElement(new Double(7.98));
    vector.addElement(new Integer(8));
     
    System.out.println("Current capacity: " +
          vector.capacity());
      
    vector.addElement(new Float(10.4));
    vector.addElement(new Integer(11));
      
    System.out.println("Current capacity: " +
      vector.capacity());
      
    System.out.println("First element: " +
         vector.firstElement());
      
    System.out.println("Last element: " +
         vector.lastElement());
     
    System.out.println("\nElements in vector: " + vector);
      
   }
}

Output of the program :


 
© 2021 Learn Java by Examples Template by Hubberspot