The indexOf() is a method of list interface, which we can use to find the index position of an element in an ArrayList.

Java List indexOf():

Just think of a situation where you want to know that an element is present. However, you don’t know at what location it is located. Or might be it is not present.  Make use of the indexOf() method then.

The ArrayList indexes start from 0; for example, if element four is present at 3rd place in the ArrayList, the indexOf(4) will be 4.

Signature:

public int indexOf(Object e);

Parameter: The element e whose index needs to be searched
Return type: int, which is the index number in case the element is present or else it returns -1.
Exception: n/a

Examples Of List IndexOf()

Here we list some of the examples which demonstrate the usage of the indexOf() method.

Example 1:

In the below example, we created an ArrayList and added some elements init. And found the index of the “hello” element using the indexOf() method.

Note: indexOf() works on case sensitive.

Source Code:

import java.util.*;
public class Main
{
  public static void main(String[] args) {
    ArrayList a1= new ArrayList();
    a1.add(3);
    a1.add("hello");
    a1.add(5);
    a1.add(6);
    a1.add(2);
    int a = a1.indexOf("hello");
    System.out.println("Index is:"+a);
  }

Output:

Index is: 1

Above, we have used the indexOf() method to find the index of the “hello”, which is 1.

Note: ArrayList indexes start from 0.

Example 2:

The indexOf() method can also be applied to duplicate elements in the list. If so, it returns the index of the first occurrence of the element.

Source code:

import java.util.*;
public class Main
{
  public static void main(String[] args) {
    ArrayList<String> a1= new ArrayList<String>();
    a1.add("hello");
    a1.add("hello");
    a1.add("Welcome");
    a1.add("hello");
    int a = a1.indexOf("hello");
    System.out.println("Index is:"+a);
  }
}

Output:

Index is: 1

Above, we have created String type and used indexOf() to find the location of “hello”. As you can see, “hello” is stored in multiple indexes above. And indexOf returns the index of the first occurrence of the string in the list.

Example 3:

The indexOf() method is exception free; it doesn’t throw an exception when looking for non-existing elements. It simply returns a negative number when it doesn’t find the index of the provided element.

Source code:

import java.util.*;
public class Main
{
  public static void main(String[] args) {
    ArrayList<Double> a1= new ArrayList<Double>();
    a1.add(2.4);
    a1.add(2.45);
    a1.add(3.46);
    a1.add(2.56);
    int a = a1.indexOf(2.1);
    System.out.println("Index is:"+a);
  }
}

Output:

Index is: -1

Above, we created type Double and used the indexOf() method to find the index of an element that does not exist, and hence the output is -1.

Conclusion:

Thus, we saw how we could use the indexOf() method to find the index of an element that is either present or not present in an ArrayList.

References:

Happy Learning 🙂

Leave A Comment