Skip to content
Related Articles

Related Articles

AbstractList lastIndexOf() method in Java with Examples

View Discussion
Improve Article
Save Article
  • Last Updated : 21 Nov, 2019
View Discussion
Improve Article
Save Article

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:




// 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 occurrence
            // 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);
        }
    }
}

Output:

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

Example 2:




// 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 occurrence
            // 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);
        }
    }
}

Output:

ArrayList : null

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!