Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

List.get(int): read a list element by index

Read an element by its zero-based index, use size() to check the bounds, and distinguish a stored null from an out-of-range index.

Version: Java SE 21

Syntax

E get(int index)
int size()
default Stream<E> stream()
public String getMessage()

E value = list.get(index); // valid when 0 <= index && index < list.size()

In Java SE 21, List.get means List.get(int) on java.util.List<E>. It returns the element of type E at the given index without removing or copying it. Indexing starts at zero, so get(1) reads the second element.

List.size/get means using size() with get(int); it is not a separate method. An index is valid when 0 <= index && index < list.size(); otherwise get throws IndexOutOfBoundsException. For a nonempty list, get(0) reads the first element and get(list.size() - 1) reads the last. An empty list has no valid index. If the list reference itself is null, calling list.size() or list.get(index) throws NullPointerException.

A valid index can hold null if the list permits null elements. Arrays.asList(10, null, 30) creates a fixed-size list backed by an array. You can read the middle element as an Integer, but auto-unboxing that null to int throws NullPointerException.

Collection.stream is inherited by List and returns a sequential stream with the collection as its source. Use get(int) when you know the position; use stream() to process elements, for example to filter out null. Here toList() collects the remaining values in source order into an unmodifiable list without changing the source.

Throwable.getMessage returns the exception's detail message as a String, or null if there is none; it does not return a stack trace. You can supply fallback text if the message is missing. Do not parse the message to detect a bounds error; handle the exception by type.

Examples

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class Main {
    public static void main(String[] args) {
        List<Integer> values = Arrays.asList(10, null, 30);
        System.out.println("First: " + values.get(0));
        System.out.println("Last: " + values.get(values.size() - 1));
        Integer middle = values.get(1);
        System.out.println("Middle: " + middle);

        List<Integer> nonNull = values.stream().filter(Objects::nonNull).toList();
        System.out.println("Non-null: " + nonNull.get(0) + ", " + nonNull.get(1));

        try {
            readAt(values, values.size());
        } catch (IndexOutOfBoundsException ex) {
            String message = ex.getMessage();
            System.out.println(message != null ? message : "Invalid index");
        }
    }

    private static <E> E readAt(List<E> list, int index) {
        if (index < 0 || index >= list.size()) {
            throw new IndexOutOfBoundsException("Invalid index: " + index);
        }
        return list.get(index);
    }
}

Result

First: 10
Last: 30
Middle: null
Non-null: 10, 30
Invalid index: 3

Index `1` is valid even though its element is `null`. An index equal to `size()` is invalid; `readAt` rejects it before calling `get(int)` and supplies the exception message explicitly.

Related lessons

  • Java · Lesson 10 · Advanced · Version: 2
  • Java · Lesson 11 · Advanced · Version: 2
  • Java · Lesson 35 · Advanced · Version: 2
  • Java · Lesson 44 · Advanced · Version: 2
  • Java · Lesson 48 · Advanced · Version: 2
  • Java · Lesson 39 · Basic · Version: 2
  • Java · Lesson 54 · Basic · Version: 2
  • Java · Lesson 58 · Basic · Version: 2
  • Java · Lesson 5 · Intermediate · Version: 2
  • Java · Lesson 6 · Intermediate · Version: 2
  • Java · Lesson 34 · Intermediate · Version: 2
  • Java · Lesson 36 · Intermediate · Version: 2
  • Java · Lesson 41 · Intermediate · Version: 2
  • Java · Lesson 44 · Intermediate · Version: 2
  • Java · Lesson 50 · Intermediate · Version: 2

Sources