Wednesday, October 22, 2025
HomeLanguagesJavaStack removeRange() method in Java with Example

Stack removeRange() method in Java with Example

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

 

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS