Arraylist removeRange() Method in Java with Examples
Last Updated : 06 Aug, 2025
The removeRange() method of the ArrayList class in Java is used to remove all elements from the list within the specified range of indices. This method removes the elements from the starting index (fromIndex) to the ending index (toIndex).
Example 1: Here, we use the removeRange() method to remove a range of integers from an ArrayList.
Java // Java program to demonstrate removeRange() method // with ArrayList of Integers import java.util.ArrayList; // custom subclass to access removeRange() class Custom extends ArrayList<Integer> { // Method to remove elements // from specified range public void removeRangeList(int s, int e) { removeRange(s, e); } } public class GFG { public static void main(String[] args) { // Creating an ArrayList Custom n = new Custom(); // Adding elements to // the ArrayList n.add(5); n.add(7); n.add(11); n.add(13); n.add(17); // Printing original list System.out.println("" + n); // Removing elements from index 1 to 4 n.removeRangeList(1, 4); System.out.println("" + n); } } Output[5, 7, 11, 13, 17] [5, 17]
Syntax of Arraylist removeRange() Method
protected void removeRange(int fromIndex, int toIndex)
Parameters:
- fromIndex: Starting index from which index elements are to be removed (inclusive).
- toIndex: The ending index of the range (exclusive).
Returns: This method does not return any value.
Exception: IndexOutOfBoundsException: If the specified range is invalid (ex- fromIndex < 0, toIndex > size, or fromIndex > toIndex).
Example 2: Here, we use the removeRange() method to remove a range of strings from an ArrayList.
Java // Java program to demonstrate removeRange() // method with ArrayList of Strings import java.util.ArrayList; // Custom subclass to // access removeRange() class Custom extends ArrayList<String> { // Method to remove elements // from specified range public void removeRangeList(int s, int e) { removeRange(s, e); } } public class GFG { public static void main(String[] args) { // Creating an ArrayList of Strings Custom n = new Custom(); // Adding elements to // the ArrayList n.add("Sweta"); n.add("Gudly"); n.add("Amiya"); n.add("Ankita"); n.add("Eva"); System.out.println("Original List: " + n); // Removing elements from // index 2 to 4 n.removeRangeList(2, 4); System.out.println("Updated List: " + n); } } OutputOriginal List: [Sweta, Gudly, Amiya, Ankita, Eva] Updated List: [Sweta, Gudly, Eva]
Example 3: In this example, an error caused when the removeRange() method is used with an invalid range (where the ending index is out of bounds).
Java // Java program to demonstrate the error in // working of removeRange() method import java.util.*; // extending the class to ArrayList since removeRange() // is a protected method public class GFG extends ArrayList<Integer> { public static void main(String[] args) { // Create an empty ArrayList GFG arr = new GFG(); // Use add() method to // add values to the list arr.add(1); arr.add(2); arr.add(3); // Trying to remove elements // in an invalid range arr.removeRange(1, 4); // Print the list after using removeRange System.out.println("The list after using removeRange: " + arr); } } Output:
Hangup (SIGHUP)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: arraycopy: length -1 is negative
at java.base/java.lang.System.arraycopy(Native Method)
at java.base/java.util.ArrayList.shiftTailOverGap(ArrayList.java:778)
at java.base/java.util.ArrayList.removeRange(ArrayList.java:773)
at GFG.main(GFG.java:22)
Explanation: In the above example, the method removeRange(1, 4) is trying to remove elements from index 1 to 4. But the valid indices are 0, 1, and 2 (the list has only 3 elements), the range is invalid that cause an IndexOutOfBoundsException to be thrown.
Note: This method is protected and is used within subclasses of ArrayList or through custom implementations.
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java