Java ArrayList removerange() Function

removerange functoin

What is an ArrayList?

Arraylist is a part of collection framework in java. It contains some inbuilt function like removeRange() function which can be found in the java.util.ArrayList Package.
It implements the List interface of the collections framework.

Here, in the page we will discuss about the ArrayList’s removeRange() Method in java. To  know more about ArrayList in java, you can click on the button given below.

ArrayList removeRange Function:

Arraylist contains several inbuilt functions that are used to perform several operations on arraylist in Java. removeRange function is also an inbuilt method of class ArrayList.
The removeRange function is used to remove all the elements of an ArrayList which lies between the given range.

Syntax:

arraylist_name.removeRange(int FirstIndex, int LastIndex)

Definition of Parameters:

replaceAll Function in Java

Return Type :

Exceptions:

clear() Function in ArrayList

clone() Function in ArrayList

add() Function in ArrayList

addall() Function in ArrayList

Example for Removing Elements From ArrayList of Integers:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        ArrayList<Integer> Prep = new ArrayList<>();
        Prep.add(1);
        Prep.add(2);
        Prep.add(3);
        Prep.add(4);
        Prep.add(5);
        
        System.out.println("Arraylist arr : " + Prep);
        Prep.removeRange(2,3);
        System.out.println("After removing Elements : " + Prep);
    }
}
Output:

/tmp/pKjTdJpUHU/Main.java:13: error: removeRange(int,int) has protected access in ArrayList
Prep.removeRange(2,3);
^

You will be getting the above error if you try to directly access or remove the elements of ArrayList class. The removeRange() method is protected. This means that it can only be accessed within the class, package and subclass.
To access the removeRange Function we can extend the Main class as the ArrayList class.

For Example:

Run
import java.util.*;

public class Main extends ArrayList<Integer>{
    public static void main(String[] args){
        Main Prep = new Main();
        Prep.add(1);
        Prep.add(2);
        Prep.add(3);
        Prep.add(4);
        Prep.add(5);
        
        System.out.println("Arraylist arr : " + Prep);
        Prep.removeRange(1,4);
        System.out.println("After removing Elements : " + Prep);
    }
}
Output:
Arraylist arr : [1, 2, 3, 4, 5]
After removing Elements : [1, 5]

In the above Example. we have created an arraylist by extending the Main class, as the Main class inherits all the properties of ArrayList class we can use it an ArrayList.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription