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() 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 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 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.

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

LEARN MORE  Java Set Tutorial with Examples

get() Method IndexOutOfBoundsException

ArrayList or List have 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

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

echo '

Leave a Reply

Your email address will not be published. Required fields are marked *