Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that Objects.equals(o, get(i)), or -1 if there is no such index.
Examples
package com.logicbig.example.arraylist;
import java.util.*;
public class IndexOfExample {
public static void main(String... args) { // Find first occurrence index ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B", "C", "B", "A")); int firstA = list.indexOf("A"); int firstB = list.indexOf("B"); int firstD = list.indexOf("D"); System.out.println("First A at: " + firstA); System.out.println("First B at: " + firstB); System.out.println("First D at: " + firstD); } }