Thursday, July 4, 2024
HomeLanguagesJavaAbstractList lastIndexOf() method in Java with Examples

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:




// 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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments