Java ArrayList subList() Method (original) (raw)

Last Updated : 12 Dec, 2024

The **subList() method of the ArrayList class in Java is used to retrieve a portion of an ArrayList between specified indices. When we perform any modifications to the **sub list, that will reflect in the original list and vice versa.

**Example 1: Here, we use the **subList() method **to extract a sublist from an ArrayList.

Java `

// Java program to demonstrate subList() // by extracting a portion of the ArrayList import java.util.ArrayList; import java.util.List;

public class GFG { public static void main(String[] args) {

    // Creating an ArrayList of flowers
    ArrayList<String> l = new ArrayList<>();
    l.add("Rose");
    l.add("Lotus");
    l.add("Lavender");
    l.add("Lilly");
    l.add("Sunflower");

    // Extracting a sublist
    List<String> s = l.subList(1, 4);

    // Printing the original list and sublist
    System.out.println("" + l);
    System.out.println("" + s);
}

}

`

Output

[Rose, Lotus, Lavender, Lilly, Sunflower] [Lotus, Lavender, Lilly]

Syntax of ArrayList subList() Method

public List subList(int fromIndex, int toIndex)

**Parameters:

**Returns Value: It returns a view of the portion of this list between the specified indices.

**Exception: This method throws the following Exceptions.

**Example 2: Here, we will **modify a sublist and will see **how the changes reflect in the original list.

Java `

// Java program to demonstrate subList() // by modifying elements in the sublist import java.util.ArrayList; import java.util.List;

public class GFG { public static void main(String[] args) {

    // Creating an ArrayList of numbers
    ArrayList<Integer> n = new ArrayList<>();
    n.add(1);
    n.add(2);
    n.add(3);
    n.add(4);
    n.add(5);

    // Creating a sublist
    List<Integer> s = n.subList(1, 4);

    // Modifying the sublist
    s.set(0, 9); // Updating the first element in sublist
    s.remove(2);  // Removing the last element in sublist

   
    System.out.println("Original List After Modification: " + n);
    System.out.println("Modified Sublist: " + s);
}

}

`

Output

Original List After Modification: [1, 9, 3, 5] Modified Sublist: [9, 3]

**Explanation: In the above example, we have created the original list starts with 5 elements i.e. [1, 2, 3, 4, 5]. The sublist created using **subList(1, 4) contains 3 elements [2, 3, 4]. After modifications, the original list becomes [1, 99, 3, 5], and the sublist becomes [99, 3].

**Example 3: Here, we will try to **use invalid indices and handle the **IndexOutOfBoundsException.

Java `

// Java program to demonstrate // IndexOutOfBoundsException in subList() import java.util.ArrayList;

public class GFG { public static void main(String[] args) {

    // Creating an ArrayList of names
    ArrayList<String> n = new ArrayList<>();
    n.add("Sweta");
    n.add("Gudly");

    try {
      
        // Trying to create a sublist 
        // with invalid indices
        n.subList(1, 5);
    } catch (IndexOutOfBoundsException e) {
        System.out.println("Error: " + e.getMessage());
    }
}

}

`

**Explanation: In this example, the **toIndex exceeds the size of the list.