Tuesday, 6 May 2014

Vector : capacity : Get the Capacity

public synchronized int capacity()
Returns the current capacity of this vector.

import java.util.*;

class VectorCapacity{
 public static void main(String args[]){
  Vector<Integer> myVector;
  myVector = new Vector<> (4);
  
  System.out.println("Capacity of the Vector is");
  System.out.println(myVector.capacity());
  
  /* Add Elements to myVector */
  for(int i=1; i < 6; i++)
   myVector.addElement(i);
  
  System.out.println("Capacity of the Vector is");
  System.out.println(myVector.capacity());
 }
}

Output
Capacity of the Vector is
4
Capacity of the Vector is
8

Capacity gets doubled when capacity increment not mentioned and size exceeds the current capacity.


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment