Java Utility Library

Java Vector - capacity() Method



The java.util.Vector.capacity() method returns the current capacity of the vector.

Syntax

public int capacity()

Parameters

No parameter is required.

Return Value

Returns the current capacity of the vector (the length of its internal data array, kept in the field elementData of this vector).

Exception

NA.

Example:

In the example below, the java.util.Vector.capacity() method is used to find out the current capacity of the vector.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector
    Vector<Integer> MyVector = new Vector<Integer>(10);

    //populating vector
    MyVector.add(10);
    MyVector.add(20);
    MyVector.add(30);
    MyVector.add(40);
    MyVector.add(50);

    System.out.println("Size of MyVector: " + MyVector.size()); 
    System.out.println("Capacity of MyVector: " + MyVector.capacity()); 
  }
}

The output of the above code will be:

Size of MyVector: 5
Capacity of MyVector: 10

❮ Java.util - Vector