Java Vector capacity() ensureCapacity() Methods Tutorial

In this section, we will learn what the Vector capacity(), ensureCapacity(), methods are and how to use them in Java.

What is Java Vector capacity() Method?

The Java Vector capacity() method is used to return the current capacity of a vector object.

Note: capacity is the maximum number of elements that a vector object can store. This value is dynamic and automatically changes when we add or remove elements from a vector object. So depending on how many elements were removed or added to a vector object, you might get a different value every time calling this method.

Java Vector capacity() Method Syntax:

public int capacity()

capacity() Method Parameters:

The method does not take an argument.

capacity() Method Return Value:

The return value of this method is the current capacity of the target vector object.

The data type of the return value is int.

capacity() Method Exceptions:

The method does not throw an exception.

Example: using Vector capacity() method

import java.util.Vector; 
public class Main {

    public static void main(String[] args){
        
        Vector <String> vector = new Vector<>();
        vector.add("Jack");
        vector.add("Omid");
        vector.add("Ellen");
        vector.add("John");

        System.out.println(vector.capacity());
    }
}

Output:

10

As you can see, here we have only 4 elements in the vector object, but the maximum capacity at this time is set to 10 (which means 6 more elements could be added to this vector object.) Of course, as we start to add more elements, the capacity will increase dynamically as well.

What is Java Vector ensureCapacity() Method?

The Java Vector ensureCapacity() method is used to set a minimum capacity for a vector object.

Note: capacity means the number of elements that a vector object can store.

Note that we say minimum capacity! This means the capacity of the target vector object can go higher than this value, but it can’t be less than the value we set for this method.

Java Vector ensureCapacity () Method Syntax:

public void ensureCapacity(int minCapacity)

ensureCapacity () Method Parameters:

The method takes one argument and that is the new value for the capacity of the target vector object.

ensureCapacity () Method Return Value:

The method does not return a value.

ensureCapacity () Method Exceptions:

The method does not throw an exception.

Example: using Vector ensureCapacity () method

import java.util.Vector; 
public class Main {

    public static void main(String[] args){
        
        Vector <String> vector = new Vector<>();
        vector.add("Jack");
        vector.add("Omid");
        vector.add("Ellen");
        vector.add("John");

        System.out.println("The current capacity of the vector object is: "+vector.capacity());
        vector.ensureCapacity(70);
        System.out.println("The current capacity after calling the ensureCapacity method: "+ vector.capacity());
    }
}

Output:

The current capacity of the vector object is: 10

The current capacity after calling the ensureCapacity method: 70

As you can see, the current capacity of this vector was set to 10, but then we’ve called the ensureCapacity() method and changed the minimum capacity to 70 and so now the minimum changed to meet our requirement.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies