AbstractList lastIndexOf() method in Java with Examples

The lastIndexOf() method of java.util.AbstractList class is used to return the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Syntax:

public int lastIndexOf(Object o)

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



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

Exception: This method throws NullPointerException if the specified element is null and this list does not permit null elements.

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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// lastIndexOf() method
// for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
        try {
  
            // Creating object of AbstractList<String>
            AbstractList<String>
                arrlist1 = new ArrayList<String>();
  
            // Populating arrlist1
            arrlist1.add("A");
            arrlist1.add("B");
            arrlist1.add("A");
            arrlist1.add("B");
            arrlist1.add("E");
  
            // print arrlist1
            System.out.println("ArrayList : "
                               + arrlist1);
  
            // getting the index of last occurence
            // using lastIndexOf() method
            int lastindex = arrlist1.lastIndexOf("A");
  
            // printing the Index
            System.out.println("Last index of A : "
                               + lastindex);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

ArrayList : [A, B, A, B, E]
Last index of A : 2

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// lastIndexOf() method
// for NullPointerException
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of AbstractList<String>
            AbstractList<String> arrlist1 = null;
  
            // print arrlist1
            System.out.println("ArrayList : "
                               + arrlist1);
  
            // getting the index of last occurence
            // using lastIndexOf() method
            System.out.println("\nTrying to get"
                               + " the index from"
                               + " null ArrayList");
            int lastindex = arrlist1.lastIndexOf("B");
  
            // printing the Index
            System.out.println("Last index of B : "
                               + lastindex);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

ArrayList : null

Trying to get the index from null ArrayList
Exception thrown : java.lang.NullPointerException


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.