ArrayList get() method - Syntax and Examples
Learn how to use the Java ArrayList's `get()` method. This tutorial explains the syntax, parameters, return value, and provides practical examples.
This reference explains ArrayList get() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.
Use this reference
Identify the documented behavior, read a complete example and check its inputs before applying it.
Introduction
The get() method in Java's ArrayList allows you to access elements stored within the list. Think of an ArrayList like a numbered shelf where you can store objects. The get() method is how you retrieve a specific object from its assigned number (index) on that shelf.
Syntax
public E get(int index)
Parameters
| Parameter | Description |
|---|---|
index |
The index of the element to retrieve. The index starts from 0. |
Return Value
The get() method returns the element at the specified index.
Examples
Example 1: Accessing a String Element
This example demonstrates how to retrieve a string from an ArrayList. We create an ArrayList of strings and then use get() to access the element at index 1.
import java.util.ArrayList;
public class GetExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
// Access the element at index 1 (which is "Bob")
String nameAtIndex1 = names.get(1);
System.out.println(nameAtIndex1); // Output: Bob
}
Bob
Explanation: We create an ArrayList called names and add three strings to it. Then, we call names.get(1) to retrieve the element at index 1. Remember that array indexes start from 0, so index 1 refers to the second element in the list.
Example 2: Accessing an Integer Element
This example shows how to access elements of type integer within an ArrayList. We create an ArrayList of integers and retrieve the value at a specified index.
import java.util.ArrayList;
public class GetIntegerExample {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
// Access the element at index 0 (which is 10)
int numberAtIndex0 = numbers.get(0);
System.out.println(numberAtIndex0); // Output: 10
}
10
Explanation: Similar to the previous example, this demonstrates accessing an element in an ArrayList but this time we are using integers. We add three integer values and then access the one at index 0.
Example 3: Handling Index Out of Bounds Exception
This example highlights what happens when you try to access an index that doesn't exist in the ArrayList (i.e., attempting to retrieve an element beyond the valid range). This results in an IndexOutOfBoundsException.
import java.util.ArrayList;
public class GetOutOfBoundExample {
public static void main(String[] args) {
ArrayList<String> colors = new ArrayList<>();
colors.add("Red");
colors.add("Green");
try {
// Attempting to access index 2, which is out of bounds.
String colorAtIndex2 = colors.get(2); // This will throw an exception
System.out.println(colorAtIndex2);
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: Index out of bounds!" + e.getMessage());
}
}
Error: Index out of bounds! Index 2 is out of bounds for length 2
Explanation: The ArrayList colors has only two elements (at indices 0 and 1). Attempting to access index 2 results in an IndexOutOfBoundsException because that index doesn't exist. The try-catch block handles this exception gracefully by printing an error message instead of crashing the program.
Using this reference
Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.
Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide.
Reference checks are optional and do not count toward course progress.