Java 25 (LTS) reference · List and ArrayList Methods
Java List get() Method
Return the element at an index (0-based). Throws IndexOutOfBoundsException for an invalid index.
Java 25 (LTS) Since Java 1.0
Syntax
list.get(index)
Parameters
| Parameter | Description |
|---|---|
indexint |
From 0 to size() - 1. |
Return value
The element.
Examples
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> langs = List.of("Java", "Kotlin", "Scala");
System.out.println(langs.get(0));
System.out.println(langs.get(langs.size() - 1));
System.out.println(langs.getLast()); // Java 21+
}
}
Output
Java Scala Scala
Run this on your computer — see “Get Started” for setup.
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> langs = List.of("Java");
try {
langs.get(5);
} catch (IndexOutOfBoundsException e) {
System.out.println("No element at index 5");
}
}
}
Output
No element at index 5
Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| Java Java 1.0 | Added |
Related
list.set()· Replace the element at an index and return the old value. The list's size doesn't change.list.indexOf()· Return the index of the first element equal to the given one, or -1. lastIndexOf() searches from the end.- Java ArrayList · tutorial