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.
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.
ArrayList : [A, B, A, B, E] Last index of A : 2
ArrayList : null Trying to get the index from null ArrayList Exception thrown : java.lang.NullPointerException
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.