Java ArrayList removeRange() Method

The ArrayList.removeRange() method in Java is a protected method used to remove a range of elements from an ArrayList. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality. Additionally, we will cover a real-world use case to illustrate its application.

Table of Contents

  1. Introduction
  2. removeRange Method Syntax
  3. How It Works
  4. Examples
    • Removing a Range of Elements
    • Extending ArrayList to Use removeRange
  5. Real-World Use Case
  6. Conclusion

Introduction

The ArrayList.removeRange() method is part of the ArrayList class in Java. It allows you to remove a specific range of elements from the list. Since it is a protected method, it cannot be accessed directly from an ArrayList instance. Instead, you need to extend the ArrayList class to use this method.

removeRange Method Syntax

The syntax for the removeRange method is as follows:

protected void removeRange(int fromIndex, int toIndex)
  • fromIndex: The index of the first element to be removed (inclusive).
  • toIndex: The index of the first element to be kept (exclusive).

How It Works

When you use the removeRange(int fromIndex, int toIndex) method, the ArrayList removes elements from the specified fromIndex (inclusive) to toIndex (exclusive). The method shifts any subsequent elements to the left (reduces their index) and adjusts the size of the list.

Examples

Removing a Range of Elements

To use the removeRange method, you need to extend the ArrayList class and create a custom method that calls removeRange.

Example

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

public class CustomArrayList<E> extends ArrayList<E> {
    public void removeRange(int fromIndex, int toIndex) {
        super.removeRange(fromIndex, toIndex);
    }

    public static void main(String[] args) {
        CustomArrayList<String> list = new CustomArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");
        list.add("Grapes");
        list.add("Mango");

        // Remove elements from index 1 to 3
        list.removeRange(1, 3);

        System.out.println("List after removeRange: " + list);
    }
}

Output:

List after removeRange: [Apple, Grapes, Mango]

Handling Invalid Range

If the specified range is invalid (e.g., fromIndex is greater than toIndex), the method will throw an IndexOutOfBoundsException.

Example

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

public class CustomArrayListWithExceptionHandling<E> extends ArrayList<E> {
    public void removeRange(int fromIndex, int toIndex) {
        try {
            super.removeRange(fromIndex, toIndex);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        CustomArrayListWithExceptionHandling<String> list = new CustomArrayListWithExceptionHandling<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");
        list.add("Grapes");
        list.add("Mango");

        // Attempt to remove elements with an invalid range
        list.removeRange(3, 1);
    }
}

Output:

Error: fromIndex > toIndex

Real-World Use Case

Removing a Batch of Orders

In an order management system, you might want to remove a batch of orders that have been processed. The removeRange() method can be used to remove these orders from the list.

Example

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

class Order {
    String id;
    String description;

    Order(String id, String description) {
        this.id = id;
        this.description = description;
    }

    @Override
    public String toString() {
        return id + ": " + description;
    }
}

public class OrderManagement extends ArrayList<Order> {
    public void removeProcessedOrders(int fromIndex, int toIndex) {
        super.removeRange(fromIndex, toIndex);
    }

    public static void main(String[] args) {
        OrderManagement orders = new OrderManagement();
        orders.add(new Order("001", "Order 1"));
        orders.add(new Order("002", "Order 2"));
        orders.add(new Order("003", "Order 3"));
        orders.add(new Order("004", "Order 4"));
        orders.add(new Order("005", "Order 5"));

        // Remove processed orders (from index 1 to 3)
        orders.removeProcessedOrders(1, 3);

        System.out.println("Orders after removing processed ones: " + orders);
    }
}

Output:

Orders after removing processed ones: [001: Order 1, 004: Order 4, 005: Order 5]

Conclusion

The ArrayList.removeRange() method in Java provides a way to remove a specific range of elements from an ArrayList. By understanding how to extend the ArrayList class and use this method, you can efficiently manage and manipulate lists in your Java applications. This method is particularly useful in real-world applications such as removing batches of processed orders or managing subsets of data.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top