Java ArrayList get() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java, get(index) returns the element stored at a position in an ArrayList without removing it. The first element is at index 0 and the last at size() - 1.

Because an ArrayList keeps its elements in an array, get is a direct lookup that takes the same short time for any index. This fast random access is the main reason to choose ArrayList; the class implements the marker interface RandomAccess to signal it.

An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe.

Syntax

Pass the index of the element you want.

Java
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
String first = list.get(0);                 // "a"
String last = list.get(list.size() - 1);    // "c"
list.get(3);                                // IndexOutOfBoundsException
CallWhat it does
list.get(0)The first element.
list.get(list.size() - 1)The last element.
for (int i = 0; i < list.size(); i++) list.get(i)Visits every element by position.
list.getFirst() / list.getLast()Java 21+: the two ends; NoSuchElementException if empty.

get(i) runs in constant time on an ArrayList. On a LinkedList the same call must walk from node to node, so index loops are much slower there.

Library declarations
Java
// java.util.ArrayList<E>
public E get(int index)

// Declared in List<E>

Parameters

ParameterDescription
index (int)Position of the element, from 0 to size() - 1.

Return Value

Return ValueDescription
EThe element at that position. Can be null if a null was stored there.

Compare with Similar Methods

Methods that work with positions:

MethodHow it differs
set(i, e)Replaces the element at i instead of reading it.
indexOf(o)The reverse question: given a value, find its index.
getFirst()Java 21+: shortcut for get(0).
iterator()Walks the elements without index arithmetic.

Java Examples

Example 1 — Read Elements by Index

Read the first, third, and last planet from a list of four.

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

public class ArrayListGetDemo {
    public static void main(String[] args) {
        List<String> planets = new ArrayList<>(Arrays.asList("Mercury", "Venus", "Earth", "Mars"));

        System.out.println("index 0: " + planets.get(0));
        System.out.println("index 2: " + planets.get(2));
        System.out.println("last: " + planets.get(planets.size() - 1));
        System.out.println("still " + planets.size() + " planets");
    }
}

How It Works

  1. Zero-based. Mercury is at index 0, so get(0) returns it, not get(1).
  2. Third element. get(2) is the third planet in the list, Earth.
  3. Last index. size() is 4, so the last valid index is 3, and get(3) returns Mars.
  4. Read only. get never removes anything; the list still holds 4 planets.

Example 2 — Sum Prices with an Index Loop

Add up all prices and remember the index of the most expensive one.

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

public class ArrayListGetLoop {
    public static void main(String[] args) {
        List<Integer> prices = new ArrayList<>(Arrays.asList(120, 45, 300, 80));
        int total = 0;
        int maxIndex = 0;

        for (int i = 0; i < prices.size(); i++) {
            int p = prices.get(i);
            total += p;
            if (p > prices.get(maxIndex)) {
                maxIndex = i;
            }
        }
        System.out.println("total = " + total);
        System.out.println("most expensive at index " + maxIndex + ": " + prices.get(maxIndex));
    }
}

How It Works

  1. Unboxing. prices.get(i) returns an Integer, which Java unboxes into the int variable p automatically.
  2. Running total. Adding 120, 45, 300, and 80 gives 545.
  3. Track a position. maxIndex remembers where the biggest price so far is, and get(maxIndex) reads that price for each comparison.
  4. Result. The largest value, 300, sits at index 2.

Example 3 — Handle an Index That Is Out of Range

Check requested seat numbers before calling get, then see what an unchecked call does.

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

public class ArrayListGetOutOfRange {
    public static void main(String[] args) {
        List<String> seats = new ArrayList<>(Arrays.asList("A1", "A2", "A3"));
        int[] requests = {1, 3, -1};

        for (int r : requests) {
            if (r >= 0 && r < seats.size()) {
                System.out.println(r + " -> " + seats.get(r));
            } else {
                System.out.println(r + " -> no such seat");
            }
        }

        try {
            seats.get(3);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("get(3) threw IndexOutOfBoundsException");
        }
    }
}

How It Works

  1. Valid range. With three seats, the only valid indexes are 0, 1, and 2.
  2. Check first. The if condition tests both ends of the range before calling get, so 1 returns A2 while 3 and -1 are reported without an exception.
  3. Unchecked call. seats.get(3) asks for a fourth seat that does not exist and throws IndexOutOfBoundsException.
  4. Fixed message. The catch block prints its own text, because the exception's message wording differs between Java versions.

Frequently Asked Questions

At 0. The first element is get(0) and the last is get(size() - 1).
get throws IndexOutOfBoundsException. Check that the index is at least 0 and less than size() before calling it.
Yes. It reads directly from the internal array, so it takes constant time for any index.
Use list.get(list.size() - 1), or list.getLast() on Java 21 and later. Make sure the list is not empty first.
No. It only reads it. Use remove(index) to read and remove at the same time.

Next: getFirst()

Read the first element (Java 21+); throws if empty.

getFirst() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful