Java Collection Tutorial - Java ArrayList.indexOf(Object o)








Syntax

ArrayList.indexOf(Object o) has the following syntax.

public int indexOf(Object o)

Example

In the following code shows how to use ArrayList.indexOf(Object o) method.

/*  w ww  . j  a v  a 2  s. c o  m*/

import java.util.ArrayList;

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

    ArrayList<String> arrlist = new ArrayList<String>(5);

    arrlist.add("G");
    arrlist.add("E");
    arrlist.add("F");
    arrlist.add("M");
    arrlist.add("from java2s.com");

    System.out.println("Size of list: " + arrlist.size());

    System.out.println(arrlist);

    // retrieving the index of element "E"
    int retval = arrlist.indexOf("E");
    System.out.println("The element E is at index " + retval);
  }
}

The code above generates the following result.