Monday, 2 January 2012

Vector

Vector

                An Vector is like an array ,which can grow in memory dynamically.it means that we can store elements into the Vector,depending on the number of elements,the memory is dynamically allotted and re-allotted accommodate all the elements. 
  • Duplicate objects are allowed
  • Insertion order preserved
  • Heterogeneous objects are allowed 
  • Null insertion is possible
  •  Vector is  synchronized .this means that when more than one thread acts simultaneously on the Vector object,the results  will be reliable
    Vector constructor:

   Vector v=new Vector();   creates an empty Vector object with default initial capacity 10.

if Vector reaches max capacity then a new Vector object will be  with

                        new   capacity=(current capacity*2)

Vector v=new Vector(int initialcapacity);creates an empty Vector object with initial capacity

Vector v=new Vector(Collection c);creates an equivalent Vector object with the given collection.

Vector Example:
 
import java.util.Vector;
import java.util.Enumeration;

public class Edemo {
 public static void main(String[] args) {
    //create a Vector object
    Vector v = new Vector();
 
    //populate the Vector
    v.add("One");
    v.add("Two");
    v.add("Three");
    v.add("Four");
   /Get Enumeration of Vector's elements using elements() method
    Enumeration e = v.elements();
  

    /*
      Enumeration provides two methods to enumerate through the elements.
      It's "hasMoreElements()" method returns true if there are more elements to
      enumerate through otherwise it returns false. Its" nextElement() "method returns
      the next element in enumeration.
    */
  
    System.out.println("Elements of the Vector are : ");
  
    while(e.hasMoreElements()){
      System.out.println(e.nextElement());


}
 

}
}



Output would be
Elements of the Vector are :
One
Two
Three
Four

No comments:

Post a Comment