Java ArrayList.subList() – Examples

In this tutorial, we will learn about the Java ArrayList.subList() method, and learn how to use this method to get a sub list from the given ArrayList, with the help of examples.

subList(int fromIndex, int toIndex)

ArrayList.subList() returns a view of the portion of this ArrayList between the specified fromIndex, inclusive, and toIndex, exclusive.

Syntax

The syntax of subList() method is

ArrayList.subList(int fromIndex, int toIndex)

where

ParameterDescription
fromIndexThe beginning index of sub list in the ArrayList. This index is inclusive of the sub list.
toIndexThe ending index of sub list in the ArrayList. This index is exclusive of the sub list.

Returns

The method returns a new List<E>.

Example 1 – subList(fromIndex, toIndex)

In this example, we will initialize an ArrayList with six string elements. We will get sub list from this ArrayList with fromIndex of 2 and toIndex of 5, using ArrayList.subList() method.

Java Program

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

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList1 = new ArrayList<String>(
				Arrays.asList("a", "b", "c", "d", "e", "f"));
		System.out.println("Original ArrayList : " + arrayList1);

		int fromIndex = 2;
		int toIndex = 5;
		List<String> arrayList2 = arrayList1.subList(fromIndex, toIndex);
		System.out.println("Sub list is        : " + arrayList2);
	}
}

Output

Original ArrayList : [a, b, c, d, e, f]
Sub list is        : [c, d, e]

Example 2 – subList(fromIndex, toIndex) – toIndex > size of ArrayList

toIndex should be less than or equal to the size of ArrayList. Else, subList() throws java.lang.IndexOutOfBoundsException.

Java Program

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

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList1 = new ArrayList<String>(
				Arrays.asList("a", "b", "c", "d", "e", "f"));
		System.out.println("Original ArrayList : " + arrayList1);

		int fromIndex = 2;
		int toIndex = 8;
		List<String> arrayList2 = arrayList1.subList(fromIndex, toIndex);
		System.out.println("Sub list is        : " + arrayList2);
	}
}

Output

Original ArrayList : [a, b, c, d, e, f]
Exception in thread "main" java.lang.IndexOutOfBoundsException: toIndex = 8
	at java.base/java.util.AbstractList.subListRangeCheck(Unknown Source)
	at java.base/java.util.ArrayList.subList(Unknown Source)
	at Example.main(Example.java:13)

Example 3 – subList(fromIndex, toIndex) – toIndex > fromIndex

fromIndex should be less than or equal toIndex. Else, subList() throws java.lang.IllegalArgumentException with the messasge “fromIndex(5) > toIndex(2)”.

Java Program

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

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList1 = new ArrayList<String>(
				Arrays.asList("a", "b", "c", "d", "e", "f"));
		System.out.println("Original ArrayList : " + arrayList1);

		int fromIndex = 5;
		int toIndex = 2;
		List<String> arrayList2 = arrayList1.subList(fromIndex, toIndex);
		System.out.println("Sub list is        : " + arrayList2);
	}
}

Output

Original ArrayList : [a, b, c, d, e, f]
Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(5) > toIndex(2)
	at java.base/java.util.AbstractList.subListRangeCheck(Unknown Source)
	at java.base/java.util.ArrayList.subList(Unknown Source)
	at Example.main(Example.java:13)

Example 4 – subList(fromIndex, toIndex) – fromIndex < 0

fromIndex should be greater than or equal to 0. Else, subList() throws java.lang.IndexOutOfBoundsException.

Java Program

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

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList1 = new ArrayList<String>(
				Arrays.asList("a", "b", "c", "d", "e", "f"));
		System.out.println("Original ArrayList : " + arrayList1);

		int fromIndex = -3;
		int toIndex = 2;
		List<String> arrayList2 = arrayList1.subList(fromIndex, toIndex);
		System.out.println("Sub list is        : " + arrayList2);
	}
}

Output

Original ArrayList : [a, b, c, d, e, f]
Exception in thread "main" java.lang.IndexOutOfBoundsException: fromIndex = -3
	at java.base/java.util.AbstractList.subListRangeCheck(Unknown Source)
	at java.base/java.util.ArrayList.subList(Unknown Source)
	at Example.main(Example.java:13)

Conclusion

In this Java Tutorial, we have learnt the syntax of Java ArrayList.subList() method, and also learnt how to use this method with the help of Java example programs.