Vector ensureCapacity() method in Java with Example

The ensureCapacity() method of Java.util.Vector class increases the capacity of this Vector instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

Syntax:

public void ensureCapacity(int minCapacity)

Parameters: This method takes the desired minimum capacity as a parameter.



Below are the examples to illustrate the ensureCapacity() method.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// ensureCapacity() method for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of Vector<Integer>
            Vector<Integer>
                vector = new Vector<Integer>();
  
            // adding element to vector
            vector.add(10);
            vector.add(20);
            vector.add(30);
            vector.add(40);
  
            // Print the Vector
            System.out.println("Vector: "
                               + vector);
  
            // ensure that the Vector
            // can hold upto 5000 elements
            // using ensureCapacity() method
            vector.ensureCapacity(5000);
  
            // Print
            System.out.println("Vector can now"
                               + " surely store upto"
                               + " 5000 elements.");
        }
  
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

chevron_right


Output:

Vector: [10, 20, 30, 40]
Vector can now surely store upto 5000 elements.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// ensureCapacity() method for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of Vector<Integer>
            Vector<String>
                vector = new Vector<String>();
  
            // adding element to vector
            vector.add("A");
            vector.add("B");
            vector.add("C");
            vector.add("D");
  
            // Print the Vector
            System.out.println("Vector: "
                               + vector);
  
            // ensure that the Vector
            // can hold upto 400 elements
            // using ensureCapacity() method
            vector.ensureCapacity(400);
  
            // Print
            System.out.println("Vector can now"
                               + " surely store upto"
                               + " 400 elements.");
        }
  
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

chevron_right


Output:

Vector: [A, B, C, D]
Vector can now surely store upto 400 elements.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.