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)
removeRange(int fromIndex, int toIndex)
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.
This method does not return any value. It only removes all the elements within the specified range.
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
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
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Vector removeRange() method in Java with Example
- Stack removeRange() method in Java with Example
- ArrayList of ArrayList in Java
- ArrayList and LinkedList remove() methods in Java with Examples
- ArrayList toArray() method in Java with Examples
- ArrayList get(index) method in Java with examples
- ArrayList clear() in Java with examples
- ArrayList iterator() method in Java with Examples
- ArrayList ensureCapacity() method in Java with Examples
- ArrayList removeAll() method in Java with Examples
- ArrayList listIterator() method in Java with Examples
- ArrayList set() method in Java with Examples
- ArrayList size() method in Java with Examples
- ArrayList subList() method in Java with Examples
- ArrayList clone() method in Java with Examples
- Java.util.ArrayList.add() Method in Java
- Java.util.ArrayList.addall() method in Java
- Java.util.Arraylist.indexOf() in Java
- Array vs ArrayList in Java
- ArrayList in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.