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 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); } } } |
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 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); } } } |
ArrayList : null Trying to get the index from null ArrayList Exception thrown : java.lang.NullPointerException
Recommended Posts:
- AbstractList set() Method in Java with Examples
- AbstractList add(E ele) method in Java with Examples
- AbstractList get() method in Java with Examples
- AbstractList iterator() method in Java with Examples
- AbstractList indexOf() method in Java with Examples
- AbstractList hashCode() method in Java with Examples
- AbstractList subList() method in Java with Examples
- AbstractList addAll() method in Java with Examples
- AbstractList clear() method in Java with Examples
- AbstractList equals() method in Java with Examples
- AbstractList listIterator() Method in Java with Examples
- AbstractList remove() Method in Java with Examples
- AbstractList add(int index, E element) method in Java with Examples
- List lastIndexOf() Method in Java with Examples
- StringBuffer lastIndexOf() method in Java with Examples
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.



