Stack ensureCapacity() method in Java with Example
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:
// 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
filter_none
Output:
Stack: [10, 20, 30, 40] Stack can now surely store upto 5000 elements.
Example 2:
// 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
filter_none
Output:
Stack: [A, B, C, D] Stack can now surely store upto 400 elements.
Recommended Posts:
- Vector ensureCapacity() method in Java with Example
- ArrayList ensureCapacity() method in Java with Examples
- StringBuffer ensureCapacity() method in Java with Examples
- StringBuilder ensureCapacity() in Java with Examples
- Stack contains() method in Java with Example
- Stack get() method in Java with Example
- Stack set() method in Java with Example
- Stack pop() Method in Java
- Stack remove(int) method in Java with Example
- Stack peek() Method in Java
- Stack removeElementAt() method in Java with Example
- Stack listIterator(int) method in Java with Example
- Stack push() Method in Java
- Stack removeAllElements() method in Java with Example
- Stack empty() Method in Java
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.



