ArrayList get() Method

In this article, we will look at the ArrayList get() method. We will learn how to get an element from a specific index from the ArrayList using the ArrayList.get() method.

1. ArrayList get() Method

Before we get into details, let’s see the signature of the get() method from the JDK.

public E get(int index) {
    Objects.checkIndex(index, size);
    return elementData(index);
}

The index shows the element to return from the list. Remember the index start from 0 and method will throw IndexOutOfBoundsException in case of invalid index. Let’s say, your array list size is 10 and if we try to get the 11th element from our list, it will throw the following exception.

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 20 out of bounds for length 5
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
	at java.base/java.util.Objects.checkIndex(Objects.java:359)
	at java.base/java.util.ArrayList.get(ArrayList.java:427)
	at org.example.App.main(App.java:48)

2. ArrayList get() Example:

Let’s see how to get the element from a specific index from the ArrayList using the get() method.

package com.javadevjournal;

import java.util.ArrayList;
import java.util.List;


public class ArrayListExample {
    public static void main(String[] args) {

        List < String > list = new ArrayList < > ();
        list.add("Sunday");
        list.add("Monday");
        list.add("Tuesday");
        list.add("Wednesday");
        list.add("Thursday");
        list.add("Friday");
        list.add("Saturday");


        String day = list.get(1); //Monday
        System.out.println(day);

        day = list.get(5); //Friday
        System.out.println(day); //Friday

        //IndexOutOfBoundsException
        list.get(10);

    }
}
Output
Monday
Friday

3. ArrayList get() Method and IndexOutOfBoundsException

Let’s see an example where using a wrong index will throw IndexOutOfBoundsException for the ArrayList.get() method call

package com.javadevjournal;

import java.util.ArrayList;
import java.util.List;


public class ArrayListExample {
    public static void main(String[] args) {

        List < String > list = new ArrayList < > ();
        list.add("Sunday");
        list.add("Monday");
        list.add("Tuesday");
        list.add("Wednesday");
        list.add("Thursday");
        list.add("Friday");
        list.add("Saturday");

        list.get(10);
    }
}

Output

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 10 out of bounds for length 7
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
	at java.base/java.util.Objects.checkIndex(Objects.java:359)
	at java.base/java.util.ArrayList.get(ArrayList.java:427)
	at org.example.App.main(App.java:25)

Summary

In this article, we saw how to use the ArrayList get() method to get an element from a specific index in the list. We also learned how to avoid the IndexOutOfBoundsException while using the get() method. You can always check the latest code from our GitHub Repository.