Java Vector.ensureCapacity(int minCapacity)

Syntax

Vector.ensureCapacity(int minCapacity) has the following syntax.

public void ensureCapacity(int minCapacity)

Example

In the following code shows how to use Vector.ensureCapacity(int minCapacity) method.


import java.util.Vector;
//from w w w  .j  a  v  a  2 s. c o  m
public class Main {
   public static void main(String args[]) {
      Vector vec = new Vector(5);
      for (int i = 0; i < 10; i++) {
         vec.add(0,i);
      }
      System.out.println("Content of the vector: "+vec);
      System.out.println("Size of the vector: "+vec.size());  
      
      // ensure the capacity of the vector and add elements
      vec.ensureCapacity(40);
      for (int i = 0; i < 10; i++) {
         vec.add(0,i);
      }    
      System.out.println(vec);
      System.out.println(vec.size());
  }    
}

The code above generates the following result.