Java 25 (LTS) reference · List and ArrayList Methods
Java List subList() Method
Return a view of part of a list, from an index up to (not including) another. Changes to the view affect the original.
Java 25 (LTS) Since Java 1.0
Syntax
list.subList(fromIndex, toIndex)
Parameters
| Parameter | Description |
|---|---|
fromIndexint |
First index (inclusive). |
toIndexint |
End index (exclusive). |
Return value
A List<E> view.
Examples
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> nums = List.of(10, 20, 30, 40, 50);
System.out.println(nums.subList(1, 4));
System.out.println(nums.subList(0, Math.min(3, nums.size())));
}
}
Output
[20, 30, 40] [10, 20, 30]
Run this on your computer — see “Get Started” for setup.
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> items = new ArrayList<>(List.of("a", "b", "c", "d"));
items.subList(1, 3).clear(); // removes b and c from the original
System.out.println(items);
}
}
Output
[a, d]
Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| Java Java 1.0 | Added |
Related
list.get()· Return the element at an index (0-based). Throws IndexOutOfBoundsException for an invalid index.- Java ArrayList · tutorial