Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Add Elements, Increase Size & Capacity of a Vector Class in JAVA Collection Framework

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 5.22k
    Comment on it

    In this blog we will understand about Vector Class in Collection Framework. We use vector class to facilitate the growable nature of collection of objects. Like array we can access any member of the collection using the integer index. The size of a Vector can be increased or decreased to facilitate the adding and deletion of items.

     

    The capacity of each vector is maintained in such a way that the capacity is always of the size of the vector size because as we add components to the vector, the size of the vector increases in chunks to the size of capacityIncrement.The Iterators returned by iterator and listIterator methods of Vector are fail-fast:

     

    Fail-Fast : It is a system in which the system reports any failure that is likely to be held in the future by throwing an exception “ConcurrentModificationException”.

     

    For example: If the structure of a Vector is modified (using any method except using the vector's default methods such as remove or add methods) any time after the creation of the Iterator, the Iterator will through “ ConcurrentModificationException” exception. On the other hand the enumerations returned by the Vector's elements method are not fail-fast.

     

    Note: The above fail-fast behavior of an Iterator is not 100% fail proof in the presence of unsynchronized concurrent modification. Hence we should avoid writing the program that have a dependency on the exception “ConcurrentModificationException”.

     

    Vector is almost similar to Arraylist except of two differences −

     

    1. Vector is synchronized.

    2. Vector also contains methods other than those existing in collections
    framework.

     

    We use Vector when we don't know in advance the size of the array and as we know that Vector can change the size dynamically.

     

    We can create vector class object in following ways:
     

    Case 1:
    Syntax:

    Vector vctr1 = new Vector();

     

    Here the created vector object has a default capacity of 10. By default vector size is doubled hence if we add 11th element the size increases to 20.


    Case 2:
    Syntax:

     

    Vector object = new Vector(int initialCapacity);


    Vector vctr2 = new Vector(5);

     

    Here the created vetor will have the capacity of 5

     

    Case 3:
    Syntax:

    Vector object= new vector(int initialcapacity, capacityIncrement);


    Vector vctr3 = new Vector(4,6);

     

    There are 2 parameters in the above Vector class constructor . The first parameters indicates capacity of the Vector and second indicates that the capacity can be increased to 6.
     

    Case 4:
    Syntax:

    Vector object= new vector(Collection col);

     

    Here the vector is created with elements of the Collection 'col'.


     

    Below is the examples of some methods of Vector that we use to manipulate and check the size and number of the vector.

    import java.util.*;
    public class DemoVector {
    
       public static void main(String args[]) {
          
          Vector vctr = new Vector(4, 2); // initial size is 5, increment is 2
    
          System.out.println("Initial size: " + vctr.size());
          System.out.println("Initial capacity: " + vctr.capacity());
          
          vctr.addElement(new String("Orange"));
          vctr.addElement(new String("Apple"));
          vctr.addElement(new String("Grapes"));
          vctr.addElement(new String("Pineapple"));
          System.out.println("Capacity after four additions: " + vctr.capacity());
    
          vctr.addElement(new Integer(5));
          System.out.println("Capacity after one more addition : " + vctr.capacity());
          
          vctr.addElement(new Double(6.08));
          vctr.addElement(new Integer(7));
          System.out.println("Capacity after two additon: " + vctr.capacity());
          
          vctr.addElement(new Float(9.4));
          
          System.out.println("Capacity after one more addition: " + vctr.capacity());
          
          vctr.addElement(new String("India"));
          vctr.addElement(new String("Africa"));
          System.out.println("First element: " + (String)vctr.firstElement());
          System.out.println("Last element: " + (String)vctr.lastElement());
          
          if(vctr.contains(new String("Apple")))
             System.out.println("Apple is in the Vector.");
             
          
          Enumeration vEnum = vctr.elements(); // Enumeration of elements in vector.
          System.out.println("\n List of elements in the vector:");
          int count = 1;
          while(vEnum.hasMoreElements())
             System.out.print("\n " + count++ + ".) " + vEnum.nextElement() + " ");
          System.out.println();
       }
    }
    
    Output:
    Initial size: 0
    Initial capacity: 4
    Capacity after four additions: 4
    Capacity after one more addition : 6
    Capacity after two additon: 8
    Capacity after one more addition: 8
    First element: Orange
    Last element: Africa
    Apple is in the Vector.
    
     List of elements in the vector:
    
     1.) Orange 
     2.) Apple 
     3.) Grapes 
     4.) Pineapple 
     5.) 5 
     6.) 6.08 
     7.) 7 
     8.) 9.4 
     9.) India 
     10.) Africa

     

    To learn about more methods of vector click the link below

    http://docs.oracle.com/javase/6/docs/api/java/util/Vector.html

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: