Java ArrayList indexOf()

The syntax of the indexOf() method is:

arraylist.indexOf(Object obj)

Here, arraylist is an object of the ArrayList class.


indexOf() Parameter

The indexOf() method takes a single parameter.

  • obj - element whose position is to be returned

If the same element obj is present in multiple location, then the position of the element that appears first in the arraylist is returned.


indexOf() Return Value

  • returns the position of the specified element from the arraylist

Note: If the specified element doesn't exist in the list, the indexOf() method returns -1.


Example 1: Get the Index of ArrayList Element

import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        // create an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();

        // insert element to the arraylist
        numbers.add(22);
        numbers.add(13);
        numbers.add(35);
        System.out.println("Number ArrayList: " + numbers);

        // find the position of 13
        int position1 = numbers.indexOf(13);
        System.out.println("Index of 13: " + position1);

        // find the position of 50
        int position2 = numbers.indexOf(50);
        System.out.println("Index of 50: " + position2);
    }
}

Output

Number ArrayList: [22, 13, 35]
Index of 13: 1
Index of 50: -1

In the above example, we have created an arraylist named numbers. Notice the expressions,

// returns 1
numbers.indexOf(13)

// returns -1
numbers.indexOf(50)

Here, the indexOf() method successfully returns the position of element 13. However, the element 50 doesn't exist in the arraylist. Hence, the method returns -1.


Example 2: Get the Position of the First Occurrence of an Element

import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        // create an ArrayList
        ArrayList<String> languages = new ArrayList<>();

        // insert element to the arraylist
        languages.add("JavaScript");
        languages.add("Python");
        languages.add("Java");
        languages.add("C++");
        languages.add("Java");
        System.out.println("Programming Languages: " + languages);

        // get the position of Java
        int position = languages.indexOf("Java");
        System.out.println("First Occurrence of Java: " + position);
    }
}

Output

Programming Languages: [JavaScript, Python, Java, C++, Java]
First Occurrence of Java: 2

In the above example, we have created an arraylist named languages. Here, we have used the indexOf() method to get the position of the element Java.

However, Java is present in two different locations in the list. In this case, the method returns the position of Java, where it appears for the first time (i.e 2).

And, if we want to get the last occurrence of Java, we can use the lastIndexOf() method. To learn more, visit Java ArrayList lastindexof().

Note: We can also get the element present in a particular location using the Java ArrayList get() method.