Arraylist removeRange() in Java with examples - GeeksforGeeks

08 Oct, 2021
The removeRange() method of ArrayList in Java is used to remove all elements within the specified range from an ArrayList object. It shifts any succeeding elements to the left. This call shortens the list 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 : There are two parameters : 1. fromIndex : starting index from which index elements are to be removed. 2. toIndex : within range[fromIndex-toIndex), all elements are removed. The parameters are of int data-type.Returns : This method does not return any value. It only removes all the elements within the specified range. Errors : indexOutOfBoundsException: if fromIndex or toIndex is out of range (fromIndex = size() or toIndex > size() or toIndex < fromIndex)Example 1 : Demonstrating the use of removeRange() method 
Java
// Java program to demonstrate the// working of removeRange() methodimport java.util.*; // extending the class to arraylist since removeRange()// is a protected methodpublic class GFG extends ArrayList<Integer> {     public static void main(String[] args)    {         // create an empty array list         GFG arr = new GFG();         // use add() method to add values in the list        arr.add(1);        arr.add(2);        arr.add(3);        arr.add(12);        arr.add(9);        arr.add(13);         // prints the list before removing        System.out.println("The list before using removeRange:" + arr);         // removing range of 1st 2 elements        arr.removeRange(0, 2);        System.out.println("The list after using removeRange:" + arr);    }}
Output:  
The list before using removeRange:[1, 2, 3, 12, 9, 13]
The list after using removeRange:[3, 12, 9, 13]
Example 2 : Program to demonstrate error  
Java
// Java program to demonstrate the error in// working of removeRange() methodimport java.util.*; // extending the class to arraylist since removeRange()// is a protected methodpublic class GFG extends ArrayList<Integer> {     public static void main(String[] args)    {         // create an empty array list         GFG arr = new GFG();         // use add() method to add values in the list        arr.add(1);        arr.add(2);        arr.add(3);         arr.removeRange(1, 4); // error as 4 is out of range         System.out.println("The list after using removeRange:" + arr);    }}
Output:  
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at java.util.ArrayList.removeRange(ArrayList.java:638)
    at GFG.main(GFG.java:25)
Note : removeRange(int fromIndex, int toIndex) method is protected method in ArrayList. A protected method is accessed in class, subclasses and in a package, but not public. Therefore we extend the class to arraylist.  
kapoorsagar226
Java - util package
Java-ArrayList
Java-Collections
Java-Functions
java-list
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Exceptions in Java
Constructors in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Comparator Interface in Java with Examples
Introduction to Java
PriorityQueue in Java
How to remove an element from ArrayList in Java?