Easy Tutorial
❮ Number Xxxvalue Arrays Compare ❯

Java ArrayList indexOf() Method

Java ArrayList

The indexOf() method returns the index value of the element in the dynamic array.

The syntax for the indexOf() method is:

arraylist.indexOf(Object obj)

Note: arraylist is an object of the ArrayList class.

Parameter Description:

Return Value

Returns the index value of the specified element's position in the dynamic array.

If the obj element appears multiple times in the dynamic array, it returns the index value of the first occurrence of obj.

If the specified element does not exist in the dynamic array, the indexOf() method returns -1.

Example

Getting the index of elements in the dynamic array:

Example

import java.util.ArrayList;

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

        // Create an array
        ArrayList<String> sites = new ArrayList<>();

        sites.add("Google");
        sites.add("tutorialpro");
        sites.add("Taobao");
        System.out.println("Website List: " + sites);

        // Find the index of the element "tutorialpro"
        int position1 = sites.indexOf("tutorialpro");
        System.out.println("Index position of tutorialpro: " + position1);

        // Find the index of the element "Weibo"
        int position2 = sites.indexOf("Weibo");
        System.out.println("Index position of Weibo: " + position2);
    }
}

Executing the above program outputs:

Index position of tutorialpro: 1
Index position of Weibo: -1

In the above example, we created a dynamic array named sites.

Note these expressions:

// Element exists in the dynamic array, returns 1
sites.indexOf("tutorialpro");

// Element does not exist in the dynamic array, returns -1
sites.indexOf("Weibo");

Getting the first occurrence of an element:

Example

import java.util.ArrayList;

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

        // Create an array
        ArrayList<String> sites = new ArrayList<>();

        sites.add("Google");
        sites.add("tutorialpro");
        sites.add("Taobao");
        sites.add("tutorialpro");
        System.out.println("Website List: " + sites);

        // Find the index of the element "tutorialpro"
        int position1 = sites.indexOf("tutorialpro");
        System.out.println("Index position of tutorialpro: " + position1);
    }
}

Executing the above program outputs:

Website List: [Google, tutorialpro, Taobao, tutorialpro]
Index position of tutorialpro: 1

In the above example, we created a dynamic array named sites. Here, we used the indexOf() method to get the position of the "tutorialpro" element.

"tutorialpro" appears at two different positions in the array list. In this case, the method returns the index of the first occurrence of "tutorialpro" (i.e., 1).

If we want to get the last occurrence of "tutorialpro", we can use the lastIndexOf() method. For more information, visit Java ArrayList lastIndexOf(). Note: We can also use the Java ArrayList get() method to retrieve the element at a specified index.

Java ArrayList

❮ Number Xxxvalue Arrays Compare ❯