Open In App

Arraylist removeRange() in Java with examples

Last Updated : 08 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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() 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 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() 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 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. 
 



Previous Article
Next Article

Similar Reads

Vector removeRange() method in Java with Example
The removeRange() method of Vector in Java is used to remove all elements within the specified range from an Vector object. It shifts any succeeding elements to the left. This call shortens the Vector by (toIndex-fromIndex) elements where toIndex is the ending index and fromIndex is the starting index within which all elements are to be removed. (I
3 min read
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 t
2 min read
How to clone an ArrayList to another ArrayList in Java?
The clone() method of the ArrayList class is used to clone an ArrayList to another ArrayList in Java as it returns a shallow copy of its caller ArrayList. Syntax: public Object clone(); Return Value: This function returns a copy of the instance of Object. Below program illustrate the Java.util.ArrayList.clone() method: Example: Java Code // Java pr
2 min read
Copy Elements of One ArrayList to Another ArrayList in Java
It is the implementation class of List Interface. It allows duplicated objects/elements and as well as maintains the insertion order. You can get the element present inside the ArrayList by the index of it now you need to pass it into the getting (index) method. You can add the elements into ArrayList using the add() method. Syntax: ArrayList Initi
7 min read
ArrayList toArray() method in Java with Examples
The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order. Declaring toArray() methodpublic Object[] toArray() or public &lt;T&gt; T[] toArray(T[] a) Parameters: This method either accepts no parameters or it takes an array T[] as a parameter which is the array into which the elements
2 min read
ArrayList get(index) Method in Java with Examples
The get() method of ArrayList in Java is used to get the element of a specified index within the list. Syntax: get(index) Parameter: Index of the elements to be returned. It is of data-type int. Return Type: The element at the specified index in the given list. Exception: It throws IndexOutOfBoundsException if the index is out of range (index=size(
2 min read
ArrayList clear() Method in Java with Examples
The clear() method of ArrayList in Java is used to remove all the elements from a list. The list will be empty after this call returns so whenever this operation has been performed all elements of the corresponding ArrayList will be deleted so it does it becomes an essential function for deleting elements in ArrayList from memory leading to optimiz
2 min read
ArrayList iterator() method in Java with Examples
The iterator() method of ArrayList class in Java Collection Framework is used to get an iterator over the elements in this list in proper sequence. The returned iterator is fail-fast. Syntax: Iterator iterator() Parameter: This method do not accept any parameter. Return Value: This method returns an iterator over the elements in this list in proper
2 min read
ArrayList ensureCapacity() method in Java with Examples
The ensureCapacity() method of java.util.ArrayList class increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. Syntax: public void ensureCapacity(int minCapacity) Parameters: This method takes the desired minimum capacity as a parameter
2 min read
ArrayList removeAll() method in Java with Examples
The removeAll() method of java.util.ArrayList class is used to remove from this list all of its elements that are contained in the specified collection. Syntax: public boolean removeAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be removed from this list. Returns Value: This method returns true if
3 min read