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
Call
What 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
Parameter
Description
index (int)
Position of the element, from 0 to size() - 1.
Return Value
Return Value
Description
E
The element at that position. Can be null if a null was stored there.
index 0: Mercury
index 2: Earth
last: Mars
still 4 planets
How It Works
Zero-based. Mercury is at index 0, so get(0) returns it, not get(1).
Third element.get(2) is the third planet in the list, Earth.
Last index.size() is 4, so the last valid index is 3, and get(3) returns Mars.
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));
}
}
Output
total = 545
most expensive at index 2: 300
How It Works
Unboxing.prices.get(i) returns an Integer, which Java unboxes into the int variable p automatically.
Running total. Adding 120, 45, 300, and 80 gives 545.
Track a position.maxIndex remembers where the biggest price so far is, and get(maxIndex) reads that price for each comparison.
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");
}
}
}
Output
1 -> A2
3 -> no such seat
-1 -> no such seat
get(3) threw IndexOutOfBoundsException
How It Works
Valid range. With three seats, the only valid indexes are 0, 1, and 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.
Unchecked call.seats.get(3) asks for a fourth seat that does not exist and throws IndexOutOfBoundsException.
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.