Sunday, December 14, 2025
HomeLanguagesJavaAbstractSequentialList lastIndexOf() method in Java with Example

AbstractSequentialList lastIndexOf() method in Java with Example

The lastIndexOf() method of java.util.AbstractSequentialList class is used to return the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest 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 parameter which is the element to search for.

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

Exception: This method throws:

  • ClassCastException: if the type of the specified element is incompatible with this list.
  • 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 AbstractSequentialList
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Creating object of AbstractSequentialList
        AbstractSequentialList<Integer>
            arrlist1 = new LinkedList<Integer>();
  
        // Populating arrlist1
        arrlist1.add(10);
        arrlist1.add(20);
        arrlist1.add(30);
        arrlist1.add(40);
        arrlist1.add(50);
  
        // print arrlist1
        System.out.println("AbstractSequentialList: "
                           + arrlist1);
  
        // getting the index of element 30
        // using lastIndexOf() method
        int index = arrlist1.lastIndexOf(30);
  
        // print the index
        System.out.println("Last Index of 30: "
                           + index);
    }
}


Output:

AbstractSequentialList: [10, 20, 30, 40, 50]
Last Index of 30: 2

Example 2:




// Java program to demonstrate lastIndexOf()
// method for AbstractSequentialList
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] args)
    {
        // Creating object of AbstractSequentialList
        AbstractSequentialList<Integer>
            arrlist1 = new LinkedList<Integer>();
  
        // Populating arrlist1
        arrlist1.add(10);
        arrlist1.add(20);
        arrlist1.add(30);
        arrlist1.add(40);
        arrlist1.add(50);
  
        // print arrlist1
        System.out.println("LinkedListlist: "
                           + arrlist1);
  
        // getting the index of element 100
        // using lastIndexOf() method
        int index = arrlist1.lastIndexOf(100);
  
        // print the index
        System.out.println("Last Index of 100: "
                           + index);
    }
}


Output:

LinkedListlist: [10, 20, 30, 40, 50]
Last Index of 100: -1
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32448 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6817 POSTS0 COMMENTS
Nicole Veronica
11954 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12031 POSTS0 COMMENTS
Shaida Kate Naidoo
6955 POSTS0 COMMENTS
Ted Musemwa
7202 POSTS0 COMMENTS
Thapelo Manthata
6899 POSTS0 COMMENTS
Umr Jansen
6883 POSTS0 COMMENTS