Related Articles

Related Articles

Stack ensureCapacity() method in Java with Example
  • Last Updated : 24 Dec, 2018

The ensureCapacity() method of Java.util.Stack class increases the capacity of this Stack 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 Stack<Integer>
            Stack<Integer>
                stack = new Stack<Integer>();
  
            // adding element to stack
            stack.add(10);
            stack.add(20);
            stack.add(30);
            stack.add(40);
  
            // Print the Stack
            System.out.println("Stack: "
                               + stack);
  
            // ensure that the Stack
            // can hold upto 5000 elements
            // using ensureCapacity() method
            stack.ensureCapacity(5000);
  
            // Print
            System.out.println("Stack can now"
                               + " surely store upto"
                               + " 5000 elements.");
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Stack: [10, 20, 30, 40]
Stack 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 Stack<Integer>
            Stack<String>
                stack = new Stack<String>();
  
            // adding element to stack
            stack.add("A");
            stack.add("B");
            stack.add("C");
            stack.add("D");
  
            // Print the Stack
            System.out.println("Stack: "
                               + stack);
  
            // ensure that the Stack
            // can hold upto 400 elements
            // using ensureCapacity() method
            stack.ensureCapacity(400);
  
            // Print
            System.out.println("Stack can now"
                               + " surely store upto"
                               + " 400 elements.");
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :