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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

list.get(index)

Parameters

ParameterDescription
index
int
From 0 to size() - 1.

Return value

The element.

Examples

First and last
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.

Out of range
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

VersionChange
Java Java 1.0Added

Was this page helpful?