Saturday 11 August 2012

Program to Implement Vector class

What is Vector class ?
         Vector class is predefined class in java.This class is present inside the java.util package.
Vector is similar like Array but having some difference between them.
  1. Vector is having many predefined method as array dont have it.
  2. Vector is synchronized.
  3. Vector is very useful when if you dont know the size of array earlier or you have to change the size over the lifetime of your program.

Vector class and their method


//Write a program to add some element into the Vector and remove any element,Display the result.
//Example of Vector class.

import java.util.*;
class VectorDemo
{
 public static void main(String[] args)
 {
  Vector v=new Vector();
  int a=args.length;
  for(int i=0;i<a;i++)
  {
   v.addElement(args[i]);
  }
  int size=v.size();
  System.out.println("Initial Size of Vector:"+size);
  String b[]=new String[size];
  v.copyInto(b);
  for(int i=0;i<size;i++)
  {
   System.out.println("Elements of Vector:"+i+":"+b[i]);
  }
  v.removeElementAt(2);
  int s=v.size();
  v.copyInto(b);
  System.out.println("After Removing Element,Size of Vector:"+s);
  for(int i=0;i<s;i++)
  {
   System.out.println("Elements of Vector:"+i+":"+b[i]);
  }
 
 }
}

Save this program as      VectorDemo.java

Compile it
                                     javac VectorDemo.java

Run it 
                                     java VectorDemo  4 5 8 9

Output
                              

If you have any trouble or wants some more program then paste your problem statement  on comment line.I will definately give you responce as early as possible.   

ShareThis

Related Posts Plugin for WordPress, Blogger...