Vectors in java - Coder's point

Monday, March 25, 2019

Vectors in java

 import java.util.*;   
 public class VectorExample1  
  {   
   public static void main(String args[])   
 {   
 Vector vec = new Vector();   
 vec.add("Tiger");   
 vec.add("Lion");   
 vec.add("Dog");   
 vec.add("Elephant");   
 System.out.println("Size is: "+vec.size());   
 System.out.println("Default capacity is: "+vec.capacity());   
 System.out.println("Vector element is: "+vec);   
 vec.addElement("Rat");   
 vec.addElement("Cat");   
 vec.addElement("Deer");   
 System.out.println("Size after addition: "+vec.size());   
 System.out.println("Capacity after addition is: "+vec.capacity());   
 System.out.println("Elements are: "+vec);   
  if(vec.contains("Tiger"))   
  {   
 System.out.println("Tiger is present at the index " +vec.indexOf("Tiger"));   
  }   
      else   
       {   
 System.out.println("Tiger is not present in the list.");   
       }   
 System.out.println("The first animal of the vector is = "+vec.firstElement());    
 System.out.println("The last animal of the vector is = "+vec.lastElement());    
  }   
 }   
OUTPUT :
Size is: 4
Default capacity is: 10
Vector element is: [Tiger, Lion, Dog, Elephant]
Size after addition: 7
Capacity after addition is: 10
Elements are: [Tiger, Lion, Dog, Elephant, Rat, Cat, Deer]
Tiger is present at the index 0
The first animal of the vector is = Tiger
The last animal of the vector is = Deer

No comments:

Post a Comment