AbstractList indexOf() method in Java with Examples

The indexOf() method of java.util.AbstractList class is used to return 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 (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Syntax:

public int indexOf(Object o)

Parameters: This method takes Object o as parameter which is the element to search for.

Return Value: This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

Below are the examples to illustrate the indexOf() method.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// indexOf() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of AbstractList<Integer>
            AbstractList<Integer>
                arrlist1 = new ArrayList<Integer>();
  
            // Populating arrlist1
            arrlist1.add(10);
            arrlist1.add(20);
            arrlist1.add(30);
            arrlist1.add(40);
            arrlist1.add(50);
  
            // print arrlist1
            System.out.println("ArrayListlist : "
                               + arrlist1);
  
            // getting the index of element 30
            // using indexOf() method
            int index = arrlist1.indexOf(30);
  
            // print the index
            System.out.println("index : " + index);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

ArrayListlist : [10, 20, 30, 40, 50]
index : 2

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// indexOf() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of AbstractList<Integer>
            AbstractList<Integer>
                arrlist1 = new ArrayList<Integer>();
  
            // Populating arrlist1
            arrlist1.add(10);
            arrlist1.add(20);
            arrlist1.add(30);
            arrlist1.add(40);
            arrlist1.add(50);
  
            // print arrlist1
            System.out.println("ArrayListlist : "
                               + arrlist1);
  
            // getting the index of element 25
            // using indexOf() method
            int index = arrlist1.indexOf(25);
  
            // print the index
            System.out.println("Index of 25 : "
                               + index);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

ArrayListlist : [10, 20, 30, 40, 50]
Index of 25 : -1

Example 3: For Null value

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// indexOf() method
// for null value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // Creating object of AbstractList<Integer>
            AbstractList<Integer>
                arrlist1 = new ArrayList<Integer>();
  
            // Populating arrlist1
            arrlist1.add(10);
            arrlist1.add(20);
            arrlist1.add(30);
            arrlist1.add(40);
            arrlist1.add(50);
  
            // print arrlist1
            System.out.println("ArrayListlist : "
                               + arrlist1);
  
            // creating null object
            Object value = null;
  
            // getting the index of element null
            // using indexOf() method
            int index = arrlist1.indexOf(value);
  
            // print the index
            System.out.println("Index of null : "
                               + index);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

ArrayListlist : [10, 20, 30, 40, 50]
Index of null : -1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.