Pin It

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 :


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot