Java ArrayList get()

The syntax of the get() method is:

arraylist.get(int index)

Here, arraylist is an object of the ArrayList class.


get() Parameter

The get() method takes single parameter.

  • index - position of the element to be accessed

get() Return Value

  • returns the element present in the specified position.
  • raises IndexOutOfBoundsException, if index is out of the range.

Example 1: get() Method with String ArrayList

Output

Programming Languages: [JavaScript, Java, Python]
Element at index 1: Java

In the above example, we have created an arraylist named languages. Here, the get() method is used to access the element present at index 1.


Example 2: get() Method with Integer ArrayList

Output

Numbers ArrayList: [22, 13, 35]
Element at index 2: 35

Here, the get() method is used to access the element at index 2.

Note: We can also get the index number of an element using the indexOf() method. To learn more, visit Java ArrayList indexOf().