In this tutorial, we will learn about the Java ArrayList removeRange() method, and learn how to use this method to remove elements in a given index range, with the help of examples.
Java ArrayList removeRange() method
ArrayList removeRange() removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
removeRange() is a protected method. So, to access this method, you may create a sub-class for the ArrayList and then access it via the object of sub-class, or you may need to create your class in the same package or sub packages of ArrayList. Accessing the method via sub-class is recommended.
Syntax
The syntax of removeRange() method with fromIndex and toIndex parameters is
removeRange(int fromIndex, int toIndex)
where
Parameter | Description |
---|---|
fromIndex | The index of first element to be removed from this ArrayList. |
toIndex | The index after last element to be removed form this ArrayList. |
Returns
The method returns void.
1. removeRange() – Remove element in ArrayList present in given range
In this example, we will create a class named Example as subclass for ArrayList<String>. So, now objects of Example can access protected classes of ArrayList<String>, like removeRange(). We will add five elements to this example object, and remove the elements in the range starting from 1
until 4
using removeRange() method.
Java Program
import java.util.ArrayList;
public class Example extends ArrayList<String> {
public static void main(String[] args) {
Example list = new Example();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
System.out.println("Original list : " + list);
int fromIndex = 1;
int toIndex = 4;
list.removeRange(fromIndex, toIndex);
System.out.println("List after removeRange() : " + list);
}
}
Output
Original list : [a, b, c, d, e]
List after removeRange() : [a, e]
2. removeRange(fromIndex, toIndex) when fromIndex < 0
If fromIndex is less than 0, removeRange() throws java.lang.ArrayIndexOutOfBoundsException.
Java Program
import java.util.ArrayList;
public class Example extends ArrayList<String> {
public static void main(String[] args) {
Example list = new Example();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
System.out.println("Original list : " + list);
int fromIndex = -2;
int toIndex = 4;
list.removeRange(fromIndex, toIndex);
System.out.println("List after removeRange() : " + list);
}
}
Output
Original list : [a, b, c, d, e]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.base/java.lang.System.arraycopy(Native Method)
at java.base/java.util.ArrayList.shiftTailOverGap(Unknown Source)
at java.base/java.util.ArrayList.removeRange(Unknown Source)
at Example.main(Example.java:16)
3. removeRange(fromIndex, toIndex) when fromIndex > toIndex
If fromIndex is greater than toIndex, removeRange() throws java.lang.IndexOutOfBoundsException.
Java Program
import java.util.ArrayList;
public class Example extends ArrayList<String> {
public static void main(String[] args) {
Example list = new Example();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
System.out.println("Original list : " + list);
int fromIndex = 4;
int toIndex = 1;
list.removeRange(fromIndex, toIndex);
System.out.println("List after removeRange() : " + list);
}
}
Output
Original list : [a, b, c, d, e]
Exception in thread "main" java.lang.IndexOutOfBoundsException: From Index: 4 > To Index: 1
at java.base/java.util.ArrayList.removeRange(Unknown Source)
at Example.main(Example.java:15)
4. removeRange(fromIndex, toIndex) when toIndex > Length of List
If toIndex
is greater than the length of the list, removeRange() throws java.lang.ArrayIndexOutOfBoundsException.
Java Program
import java.util.ArrayList;
public class Example extends ArrayList<String> {
public static void main(String[] args) {
Example list = new Example();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
System.out.println("Original list : " + list);
int fromIndex = 1;
int toIndex = 9;
list.removeRange(fromIndex, toIndex);
System.out.println("List after removeRange() : " + list);
}
}
Output
Original list : [a, b, c, d, e]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.base/java.lang.System.arraycopy(Native Method)
at java.base/java.util.ArrayList.shiftTailOverGap(Unknown Source)
at java.base/java.util.ArrayList.removeRange(Unknown Source)
at Example.main(Example.java:15)
Conclusion
In this Java Tutorial, we have learnt the syntax of Java ArrayList removeRange() method, and also learnt how to use this method with the help of examples.