coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

ArrayList removeRange() method in Java

In this article, we will see the removeRange() method of ArrayList class in Java. ArrayList class in Java. This method is used to remove a range of elements from the ArrayList.

Introduction

ArrayList is a widely used class to store and retrieve data in a collection framework.

ArrayList is an ordered collection i.e. it maintains the insertion order of the elements. Also, it allows duplicate elements in the list.

We have seen how to remove elements from the ArrayList. Today, we will see how to remove a range of elements from the ArrayList.

Also, We have seen a few methods of the ArrayList class before like add(), addAll(), remove(), set(), iterate(), clear(), contains() and get() method.

Today we will see the removeRange() method.

ArrayList removeRange(int fromIndex,int toIndex) method

The removeRange(int fromIndex, int toIndex) method is a protected method in ArrayList. A protected method is accessed in class, subclasses and in a package, but not public.

The removeRange(int fromIndex, int toIndex) method is used to remove the range of elements from the subclass of ArrayList. It removes all the elements from index fromIndex, inclusive, to toIndex exclusive.

The method signature is given below.

1
protected void removeRange(int fromIndex, int toIndex)  

As shown in the above method signature, it accepts two parameters.

Parameter Description
fromIndex index of the first element to be removed
toIndex index after the last element to be removed

The method has a return type as a void. It means it does not return anything.

I have given an example Java program to remove a range of elements from the subclass that extends ArrayList in Java using the removeRange() method.

Before starting the example, please remember that the parameter fromIndex is inclusive. It means the method will start removing the elements from the fromIndexth element.

And the parameter toIndex is exclusive. It means the method will remove elements till the toIndex index. It will not remove the element at the toIndexth index.

Let’s see the Java program for a better understanding.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.gaurav.ExProject.ArrayList;

import java.util.ArrayList;
import java.util.List;

/**
 * A Java program to remove the range of elements from 
 * the MyArraylist (a class which extends the ArrayList)
 * using the removeRange() method.
 * 
 * The removeRange() method is protected and is accessed in class,
 * subclasses and in a package, but not public.
 * 
 * @author Gaurav Kukade at coderolls.com
 *
 */
public class MyArrayList extends ArrayList<String> {

  public static void main(String[] args) {
  
    MyArrayList myArrayList = new MyArrayList();
    
    myArrayList.add("Monday");
    myArrayList.add("Tuesday");
    myArrayList.add("Wednesday");
    myArrayList.add("Thursday");
    myArrayList.add("Friday");
    myArrayList.add("Saturday");
    myArrayList.add("Sunday");
    
    System.out.println("ArrayList before removing a range of elements:\n"+ myArrayList);
    
    myArrayList.removeRange(2, 4);
    
    System.out.println("\nArrayList after removing a range of elements:\n"+ myArrayList); 
  }
}

Output:

1
2
3
4
5
ArrayList before removing a range of elements:
[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]

Arraylist after removing a range of elements:
[Monday, Tuesday, Friday, Saturday, Sunday]

Conclusion

We can use the removeRange(int fromIndex, int toIndex) method to remove the range of elements from the subclass of ArrayList. This range will start from the element at index fromIndex to the element before the toIndex index.

The parameter fromIndex is inclusive and toIndex is exclusive.


Important Note:

The 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.


The example java program used in the above article can be found at this GitHub repository.

Please write your thoughts in the comment section below.

Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus