A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/abstractlist-sublist-method-in-java-with-examples/ below:

AbstractList subList() method in Java with Examples

AbstractList subList() method in Java with Examples

Last Updated : 11 Jul, 2025

The

subList()

method of

java.util.AbstractList

class is used to return a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations.

Syntax:
public List<E> subList(int fromIndex, int toIndex)
Parameters:

This method takes the following argument as a parameter.

Returns Value:

This method returns a

view of the specified range

within this list.

Exception:

This method throws the following Exception.

Below are the examples to illustrate the subList() method:

Example 1: Java
// Java program to demonstrate
// subList() method for String value

import java.util.*;

public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {

        try {

            // Creating object of AbstractList<Integer>
            AbstractList<String>
                arrlist = new ArrayList<String>();

            // Populating arrlist1
            arrlist.add("A");
            arrlist.add("B");
            arrlist.add("C");
            arrlist.add("D");
            arrlist.add("E");

            // print arrlist
            System.out.println("Original AbstractList: "
                               + arrlist);

            // getting the subList
            // using subList() method
            List<String> arrlist2 = arrlist.subList(2, 4);

            // print the subList
            System.out.println("Sublist of AbstractList: "
                               + arrlist2);
        }

        catch (IndexOutOfBoundsException e) {
            System.out.println(e);
        }

        catch (IllegalArgumentException e) {
            System.out.println(e);
        }
    }
}
Output:
Original AbstractList: [A, B, C, D, E]
Sublist of AbstractList: [C, D]
Example 2:

For IndexOutOfBoundsException

Java
// Java program to demonstrate
// subList() method for IndexOutOfBoundsException

import java.util.*;

public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {

        try {

            // Creating object of AbstractList<Integer>
            AbstractList<String>
                arrlist = new ArrayList<String>();

            // Populating arrlist1
            arrlist.add("A");
            arrlist.add("B");
            arrlist.add("C");
            arrlist.add("D");
            arrlist.add("E");

            // print arrlist
            System.out.println("Original AbstractList: "
                               + arrlist);

            // getting the subList
            // using subList() method
            System.out.println("\nEnd index value is out of range");
            List<String> arrlist2 = arrlist.subList(2, 7);

            // print the subList
            System.out.println("Sublist of AbstractList: "
                               + arrlist2);
        }

        catch (IndexOutOfBoundsException e) {
            System.out.println(e);
        }

        catch (IllegalArgumentException e) {
            System.out.println(e);
        }
    }
}
Output:
Original AbstractList: [A, B, C, D, E]

End index value is out of range
java.lang.IndexOutOfBoundsException: toIndex = 7
Example 3:

For

IllegalArgumentException Java
// Java program to demonstrate
// subList() method for IllegalArgumentException

import java.util.*;

public class GFG1 {
    public static void main(String[] argv) throws Exception
    {

        try {

            // Creating object of AbstractList<Integer>
            AbstractList<String>
                arrlist = new ArrayList<String>();

            // Populating arrlist1
            arrlist.add("A");
            arrlist.add("B");
            arrlist.add("C");
            arrlist.add("D");
            arrlist.add("E");

            // print arrlist
            System.out.println("Original AbstractList: "
                               + arrlist);

            // getting the subList
            // using subList() method
            System.out.println("\nEndpoint indices "
                               + "are out of order"
                               + " (fromIndex > toIndex)");
            List<String> arrlist2 = arrlist.subList(7, 2);

            // print the subList
            System.out.println("Sublist of AbstractList: "
                               + arrlist2);
        }

        catch (IndexOutOfBoundsException e) {
            System.out.println(e);
        }

        catch (IllegalArgumentException e) {
            System.out.println(e);
        }
    }
}
Output:
Original AbstractList: [A, B, C, D, E]

Endpoint indices are out of order (fromIndex > toIndex)
java.lang.IllegalArgumentException: fromIndex(7) > toIndex(2)


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4