English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input Output (I/O)

Java Reader/Writer

Java Other Topics

Java ArrayList indexOf() Method Usage and Example

Java ArrayList Methods

The Java ArrayList indexOf() method returns the position of the specified element in the ArrayList.

The syntax of indexOf() method is:

arraylist.indexOf(Object obj)

indexOf() parameter

  • obj - The element to return its position

 If the same element obj exists in multiple positions, then return the position of the first occurrence of the element in the ArrayList.

indexOf() return value

  • Return the position of the specified element from the ArrayList

Note:If the specified element does not exist in the list, the indexOf() method returns -1

Example1:Get the index of an ArrayList element

import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        //Create ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();
        // Insert elements into the ArrayList
        numbers.add(22);
        numbers.add(13);
        numbers.add(35);
        System.out.println("Number ArrayList: ", + numbers);
        //Find the index value of13of
        int position1 = numbers.indexOf(13);
        System.out.println("13the position: " + position1);
        //Find the index value of5The position of 0
        int position2 = numbers.indexOf(50);
        System.out.println("5The index value of 0: " + position2);
    }
}

Output Result

Number ArrayList: [22, 13, 35]
13The index value of: 1
5The index value of 0: -1

In the above example, we created an ArrayList named numbers. Note these expressions,

// Return 1
numbers.indexOf(13)
// Return -1
numbers.indexOf(5(0)

Here, the indexOf() method successfully returns the position of the element13the position.50The element does not exist in the ArrayList. Therefore, this method returns-1

Example2:Get the position of the first occurrence of an element

import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        //Create ArrayList
        ArrayList<String> languages = new ArrayList<>();
        //Insert elements into the ArrayList
        languages.add("JavaScript");
        languages.add("Python");
        languages.add("Java");
        languages.add("C",++");
        languages.add("Java");
        System.out.println("Programming language: ", + languages);
        //Get the position of Java
        int position = languages.indexOf("Java");
        System.out.println("First occurrence of Java: ", + position);
    }
}

Output Result

Programming Language: [JavaScript, Python, Java, C++, Java]
First occurrence of Java: 2

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

However, Java exists at two different positions in the list. In this case, the method returns the first occurrence of Java (i.e.,2position).

And, if we want to get the last occurrence of Java, we can use the lastIndexOf() method.

NoteWe can also useJava ArrayList get()method to get the element at the specified position.

Java ArrayList Methods