Stack removeRange() method in Java with Example Last Updated : 08 Oct, 2021 Comments Improve Suggest changes Like Article Like Report The removeRange() method of Stack in Java is used to remove all elements within the specified range from an Stack object. It shifts any succeeding elements to the left. This call shortens the stack by (toIndex-fromIndex) elements where toIndex is the ending index and fromIndex is the starting index within which all elements are to be removed. (If toIndex==fromIndex, this operation has no effect) Syntax: removeRange(int fromIndex, int toIndex) Parameters: This method takes two parameters: fromIndex: starting index from which index elements are to be removed.toIndex: within range[fromIndex-toIndex), all elements are removed. Return Value: This method does not return any value. It only removes all the elements within the specified range. Exceptions: This method throws indexOutOfBoundsException if fromIndex or toIndex is out of range (fromIndex = size() or toIndex > size() or toIndex < fromIndex)Below examples illustrate the Stack.removeRange() method: Example 1 : Demonstrating the use of removeRange() method Java // Java program to demonstrate the // working of removeRange() method import java.util.*; // extending the class to stackyastack since removeRange() // is a protected method public class GFG extends Stack<Integer> { public static void main(String[] args) { // create an empty stack GFG stack = new GFG(); // use add() method to add values in the stack stack.add(1); stack.add(2); stack.add(3); stack.add(12); stack.add(9); stack.add(13); // prints the stack before removing System.out.println("The stack before using removeRange:" + stack); // removing range of 1st 2 elements stack.removeRange(0, 2); System.out.println("The stack after using removeRange:" + stack); } } Output: The stack before using removeRange:[1, 2, 3, 12, 9, 13] The stack after using removeRange:[3, 12, 9, 13] Example 2 : Program to demonstrate error Java // Java program to demonstrate the error in // working of removeRange() method import java.util.*; // extending the class to stackyastack since removeRange() // is a protected method public class GFG extends Stack<Integer> { public static void main(String[] args) { // create an empty stack stack GFG stack = new GFG(); // use add() method to add values in the stack stack.add(1); stack.add(2); stack.add(3); try { // error as 4 is out of range stack.removeRange(1, 4); System.out.println("The stack after using removeRange:" + stack); } catch (Exception e) { System.out.println(e); } } } Output: java.lang.ArrayIndexOutOfBoundsException Comment More infoAdvertise with us Next Article Stack removeRange() method in Java with Example C code_r Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-Stack +1 More Practice Tags : JavaJava-Collections Similar Reads 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 t 2 min read Stack elements() method in Java with Example The Java.util.Stack.elements() method of Stack class in Java is used to get the enumeration of the values present in the Stack. Syntax: Enumeration enu = Stack.elements() Parameters: The method does not take any parameters. Return value: The method returns an enumeration of the values of the Stack. 2 min read Stack clone() method in Java with Example The clone() method of Stack class is used to return a shallow copy of this Stack. It just creates a copy of the Stack. The copy will have a reference to a clone of the internal data array but not a reference to the original internal data array. Syntax: Stack.clone()Parameters: The method does not ta 2 min read Stack iterator() method in Java with Example The Java.util.Stack.iterator() method is used to return an iterator of the same elements as that of the Stack. The elements are returned in random order from what was present in the stack. Syntax: Iterator iterate_value = Stack.iterator(); Parameters: The function does not take any parameter. Return 2 min read Stack copyInto() method in Java with Example The java.util.Stack.copyInto() method is used to copy all of the components from this Stack to another Stack, having enough space to hold all of the components of the Stack. It is to be noted that the index of the elements remains unchanged. The elements present in the Stack are replaced by the elem 2 min read Stack elementAt() method in Java with Example The Java.util.Stack.elementAt(int pos) method is used to fetch or retrieve an element at a specific index from a Stack. Syntax: Stack.elementAt(int pos) Parameters: This method accepts a mandatory parameter pos of integer data type that specifies the position or index of the element to be fetched fr 2 min read Stack isEmpty() method in Java with Example The Java.util.Stack.isEmpty() method in Java is used to check and verify if a Stack is empty or not. It returns True if the Stack is empty else it returns False. Syntax: Stack.isEmpty() Parameters: This method does not take any parameter. Return Value: This function returns True if the Stackis empty 2 min read Stack insertElementAt() method in Java with Example The Java.util.Stack.insertElementAt(element, index) method is used to insert a particular element at the specified index of the Stack. Both the element and the position is passed as the parameters. If an element is inserted at a specified index, then all the elements are pushed upward by one and hen 2 min read Stack clear() method in Java with Example The Java.util.Stack.clear() method is used to remove all the elements from a Stack. Using the clear() method only clears all the element from the Stack and does not delete the Stack. In other words, we can say that the clear() method is used to only empty an existing Stack. Syntax: Stack.clear() Par 2 min read Stack indexOf(Object, int) method in Java with Example The Java.util.Stack.indexOf(Object element, int index) method is used to the index of the first occurrence of the specified element in this Stack, searching forwards from index, or returns -1 if the element is not found. More formally, returns the lowest index i such that (i >= index && O 2 min read Like