Java ArrayList subList() Method

The ArrayList.subList(int fromIndex, int toIndex) method in Java is used to obtain a view of a portion of the ArrayList. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. subList Method Syntax
  3. Examples
    • Getting a SubList
    • Modifying the SubList
    • Handling IndexOutOfBoundsException
  4. Conclusion

Introduction

The ArrayList.subList(int fromIndex, int toIndex) method is a member of the ArrayList class in Java. It allows you to obtain a view of a specified range within the ArrayList. The view is backed by the original list, so changes to the sublist are reflected in the original list and vice versa.

subList Method Syntax

The syntax for the subList method is as follows:

public List<E> subList(int fromIndex, int toIndex)
  • fromIndex: The low endpoint (inclusive) of the sublist.
  • toIndex: The high endpoint (exclusive) of the sublist.

The method returns a view of the specified range within the ArrayList.

Examples

Getting a SubList

You can use the subList method to obtain a sublist of a specified range from the ArrayList.

Example

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

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

        // Get a sublist from index 1 (inclusive) to index 4 (exclusive)
        List<String> subList = list.subList(1, 4);

        System.out.println("Original ArrayList: " + list);
        System.out.println("SubList: " + subList);
    }
}

Output:

Original ArrayList: [Apple, Banana, Orange, Grapes, Pineapple]
SubList: [Banana, Orange, Grapes]

Modifying the SubList

Changes made to the sublist are reflected in the original ArrayList and vice versa.

Example

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

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

        // Get a sublist from index 1 (inclusive) to index 4 (exclusive)
        List<String> subList = list.subList(1, 4);

        System.out.println("Original ArrayList before modification: " + list);
        System.out.println("SubList before modification: " + subList);

        // Modify the sublist
        subList.set(1, "Blueberry");

        System.out.println("Original ArrayList after modification: " + list);
        System.out.println("SubList after modification: " + subList);
    }
}

Output:

Original ArrayList before modification: [Apple, Banana, Orange, Grapes, Pineapple]
SubList before modification: [Banana, Orange, Grapes]
Original ArrayList after modification: [Apple, Banana, Blueberry, Grapes, Pineapple]
SubList after modification: [Banana, Blueberry, Grapes]

Handling IndexOutOfBoundsException

Attempting to create a sublist with invalid indices (e.g., fromIndex is greater than toIndex, or either index is out of bounds) will throw an IndexOutOfBoundsException.

Example

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

public class SubListInvalidExample {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        // Attempt to create a sublist with invalid indices
        try {
            List<String> subList = list.subList(2, 1);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Error: " + e.getMessage());
        }

        // Attempt to create a sublist with out of bounds indices
        try {
            List<String> subList = list.subList(1, 4);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Output:

Error: fromIndex(2) > toIndex(1)
Error: toIndex = 4

Conclusion

The ArrayList.subList(int fromIndex, int toIndex) method in Java provides a convenient way to obtain a view of a portion of an ArrayList. By understanding how to use this method, you can efficiently work with subsets of your lists in Java applications. It's important to handle potential IndexOutOfBoundsException by ensuring that the specified indices are valid before attempting to create a sublist.

Comments