Java ArrayList.get(int index)

Syntax

ArrayList.get(int index) has the following syntax.

public E get(int index)

Example

In the following code shows how to use ArrayList.get(int index) method.


//  w ww .  j  a  v a  2 s.  co  m

import java.util.ArrayList;

public class Main {
   public static void main(String[] args) {
    ArrayList<Integer>  arrlist = new ArrayList<Integer> (5);

    arrlist.add(15);
    arrlist.add(22);
    arrlist.add(30);
    arrlist.add(40);

    System.out.println(arrlist);
  
    // retrieves element at 4th postion
    
    int retval=arrlist.get(3);
    
  System.out.println("Retrieved element is = " + retval);     
  }
}

The code above generates the following result.