Vector Example program

Java Vector Class

  • Java Vector class comes under the java.util package. The vector class implements a growable array of objects. Like an array, it contains the component that can be accessed using an integer index.
  • Vector is very useful if we don't know the size of an array in advance or we need one that can change the size over the lifetime of a program.
  • Vector implements a dynamic array that means it can grow or shrink as required. It is similar to the ArrayList, but with two differences-

  • Vector is synchronized.
  • The vector contains many legacy methods that are not the part of a collections framework

Three ways to create vector class object:

1.Vector vec=new Vector()

Method 2:
Syntax: Vector object= new Vector(int initialCapacity)

Method 3:


Vector object= new vector(int initialcapacity, capacityIncrement)
Example program :
  1. import java.util.*;  
  2. public class VectorExample1 {  
  3.        public static void main(String args[]) {  
  4.           //Create an empty vector with initial capacity 4  
  5.           Vector<String> vec = new Vector<String>(4);  
  6.           //Adding elements to a vector  
  7.           vec.add("Tiger");  
  8.           vec.add("Lion");  
  9.           vec.add("Dog");  
  10.           vec.add("Elephant");  
  11.           //Check size and capacity  
  12.           System.out.println("Size is: "+vec.size());  
  13.           System.out.println("Default capacity is: "+vec.capacity());  
  14.           //Display Vector elements  
  15.           System.out.println("Vector element is: "+vec);  
  16.           vec.addElement("Rat");  
  17.           vec.addElement("Cat");  
  18.           vec.addElement("Deer");  
  19.           //Again check size and capacity after two insertions  
  20.           System.out.println("Size after addition: "+vec.size());  
  21.           System.out.println("Capacity after addition is: "+vec.capacity());  
  22.           //Display Vector elements again  
  23.           System.out.println("Elements are: "+vec);  
  24.           //Checking if Tiger is present or not in this vector         
  25.             if(vec.contains("Tiger"))  
  26.             {  
  27.                System.out.println("Tiger is present at the index " +vec.indexOf("Tiger"));  
  28.             }  
  29.             else  
  30.             {  
  31.                System.out.println("Tiger is not present in the list.");  
  32.             }  
  33.             //Get the first element  
  34.           System.out.println("The first animal of the vector is = "+vec.firstElement());   
  35.           //Get the last element  
  36.           System.out.println("The last animal of the vector is = "+vec.lastElement());   
  37.        }  
  38. }  

Comments

  1. Sir Java structure' s post chyendii

    ReplyDelete
  2. Tiger
    Lion
    Dog
    Elephant
    Rat
    Cat
    Deer
    Out put ila vasthe
    Naku deer anadhi tiger ki lion ki madyalo ravali ante Emily cheyali sir

    ReplyDelete

Post a Comment