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.
Syntax:
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.
Example 1:
// Java program to demonstrate // setSize() method for Integer value import java.util.*; public class GFG1 { public static void main(String[] argv) { 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); // Print the current size of Stack System.out.println("Current size of Stack: " + stack.size()); // Change the size to 10 stack.setSize(10); // Print the current size of Stack System.out.println("New size of Stack: " + stack.size()); } catch (Exception e) { System.out.println("Exception thrown : " + e); } } } |
chevron_right
filter_none
Output:
Stack: [10, 20, 30, 40] Current size of Stack: 4 New size of Stack: 10
Example 2:
// Java program to demonstrate // setSize() method for String value import java.util.*; public class GFG1 { public static void main(String[] argv) { 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); // Print the current size of Stack System.out.println("Current size of Stack: " + stack.size()); // Change the size to -1 stack.setSize(-1); // Print the current size of Stack System.out.println("New size of Stack: " + stack.size()); } catch (Exception e) { System.out.println("Exception thrown : " + e); } } } |
chevron_right
filter_none
Output:
Stack: [A, B, C, D] Current size of Stack: 4 Exception thrown : java.lang.ArrayIndexOutOfBoundsException: -1
Recommended Posts:
- Vector setSize() method in Java with Example
- Stack set() method in Java with Example
- Stack get() method in Java with Example
- Stack contains() method in Java with Example
- Stack pop() Method in Java
- Stack setElementAt() method in Java with Example
- Stack addElement(E) method in Java with Example
- Stack push() Method in Java
- Stack peek() Method in Java
- Stack firstElement() method in Java with Example
- Stack hashCode() method in Java with Example
- Stack search() Method in Java
- Stack toString() method in Java with Example
- Stack iterator() method in Java with Example
- Stack elements() method in Java with Example
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.



