Published Feb 1, 2023
Contribute to Docs

The .get() method retrieves the element at some index in an ArrayList.



The element at index index in arrayListInstance is returned. If the index is out of range, an IndexOutOfBoundsException will be thrown.


In the example below, an empty ArrayList, studentList, is created and populated with several elements of type String. The .get() method retrieves the students at indexes 0 and 1.

// Students.java
import java.util.ArrayList;
public class Students {
public static void main(String[] args) {
ArrayList<String> studentList = new ArrayList<>();
// Add new values to the studentList
// `.get()` method extracts the element's value
String StudentOne = studentList.get(0);
String StudentTwo = studentList.get(1);
// Output ArrayList and searched elements
System.out.println("ArrayList: " + studentList);
System.out.println("Searched Elements: " + StudentOne + " and " + StudentTwo);

The output will look like this:

ArrayList: [John, Lily, Samantha, Tony]
Searched Elements: John and Lily

All contributors

Looking to contribute?

Learn Java on Codecademy