ArrayList indexOf(Object o) method in java

Let’s learn ArrayList indexOf(Object o) method in java.

ArrayList indexOf(Object o) method in java

indexOf(Object o) method of ArrayList class returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

Syntax:

public int indexOf(Object o)

Parameters:

o element to search for.

Also read – ArrayList in java

Now let’s see example on ArrayList indexOf(Object o) method.

import java.util.ArrayList;
public class ArrayListIndexOfMethodExample
{
   public static void main(String[] args)
   {
      ArrayList<Integer> al = new ArrayList<Integer>(5);
      al.add(8);
      al.add(6);
      al.add(5);
      al.add(7);
      al.add(9);
      System.out.print("ArrayList values are: ");
      for(Integer num : al)
      {
         System.out.print(num);
         System.out.print(" ");
      }
      // indexOf() method to find index of 5
      int position = al.indexOf(5);
      System.out.println("\nElement 5 is at index: " + position);
   }
}


Output:

ArrayList values are: 8 6 5 7 9
Element 5 is at index: 2