Wednesday, April 28, 2010

Vector Example using java

The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.

Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement.


Java Program to implement Vector :
import java.util.Vector;

public class Vectors {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {

 // TODO Auto-generated method stub
 Vector v = new Vector();

 v.addElement("1");
 v.addElement("4");
 v.addElement("2");
 v.addElement("3");

 //System.out.println(v);
 Enumeration e = v.elements();
 while(e.hasMoreElements()){
  System.out.println(e.nextElement());
 }
 }
}

Author : Anuj Patel
Blog : http://goldenpackagebyanuj.blogspot.in/