Java ArrayList Get() Method Tutorial with Examples – POFTUT

Java ArrayList Get() Method Tutorial with Examples


Java programming language provides List and ArrayList classes and objects in order to store multiple items in a single variable or structure. These List and ArrayList objects provide the get() method which is simply used to get an item from the List or ArrayList.

get() Method Syntax

The get() method of the ArrayList or List has the following syntax. The index numbers in Lists and ArrayLists start from 0 which means the first item or element index number is 0.

ITEM OBJECT.get(int INDEX)
  • ITEM is the object which will be returned from the given OBJECT with the given INDEX number.
  • OBJECT is the list or ArrayList object which contains items.
  • int INDEX specifies the index number of the item we want to get to ITEM from the OBJECT.

mylist.get(5)

  • mylist is an ArrayList or List which contains multiple items.
  • get(5) will return the item which index is 5.

get() Method Example with ArrayList

We will make an example with ArrayList and get() method. We will create an ArrayList named mylist and add items 10,20,30,40,50,60 to this list. Then we will use get() function to return the 2nd element of the ArrayList.

import java.util.*;

public class Java_List_Get {
        public static void main(String[] args)
        {
                // creating an Empty Integer List 
                List<Integer> mylist = new ArrayList<Integer>(4);

                // using add() to initialize values 
                // [10, 20, 30, 40] 
                mylist.add(10);
                mylist.add(20);
                mylist.add(30);
                mylist.add(40);
                mylist.add(50);
                mylist.add(60);

                System.out.println("List: " + mylist);

                // element at index 2 
                int element = mylist.get(2);

                System.out.println("The element at index 2 is " + element);

                // element at index 5 
                int element = mylist.get(5);
                System.out.println("The element at index 5 is " + element);
        }
}

This example get() method will return or output 30 as the index number 2 and 60 for index number 5.

LEARN MORE  How To Define Array In Programming Languages Java, JavaScript, PHP, C, C++, Python, C#, PowerShell?

get() Method IndexOutOfBoundsException

ArrayList or List has a limited number of elements. If the get() function is used to request an item with an index number which is higher than the List or ArrayList this will create an exception. The exception name or type is IndexOutOfBoundsException which is like below.

import java.util.*;

public class Java_List_Get {
        public static void main(String[] args)
        {
                // creating an Empty Integer List 
                List<Integer> mylist = new ArrayList<Integer>(4);

                // using add() to initialize values 
                // [10, 20, 30, 40] 
                mylist.add(10);
                mylist.add(20);
                mylist.add(30);
                mylist.add(40);
                mylist.add(50);
                mylist.add(60);

                System.out.println("List: " + mylist);

                // element at index 2 
                int element = mylist.get(10);

                System.out.println("The element at index 10 is " + element);
        }
}

We can see below that line number 22 throws the exception which is explained as Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 10 out of bounds for length 6. This exception message simply says that the requested index number 10 does not exist in the list because there are only 6 elements.

Java IndexOutOfBoundsException Exception
Java IndexOutOfBoundsException Exception

Leave a Comment