Stack setSize() method in Java with Example
The setSize() method of Java.util.Stack class changes the size of this Stack instance to the size passed as the parameter.
public void setSize(int size)
Parameters: This method takes the new size as a parameter.
Exception: This method throws ArrayIndexOutOfBoundsException if the new size is negative.
Below are the examples to illustrate the setSize() method.
Stack: [10, 20, 30, 40] Current size of Stack: 4 New size of Stack: 10
Stack: [A, B, C, D] Current size of Stack: 4 Exception thrown : java.lang.ArrayIndexOutOfBoundsException: -1
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.