Open In App

Java.util.LinkedList.indexOf(), lastIndexof() in Java

Last Updated : 06 Jan, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

Linked list library also offers to depict the first and last index of element that has to be found using the indexOf() and lastIndexOf() functions respectively. They offer a variety as the direct access is not available in the conventionally made linked list, hence knowledge of it is useful.
1. indexOf(Object o) : 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.

Declaration : 
   public int indexOf(Object o)
Parameters : 
   o :  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.




// Java code to demonstrate the application
// of indexOf() in linked list
import java.util.*;
public class LinkedListIndexApp {
  
public static void main(String[] args)
    {
  
        // Declaring a LinkedList
        LinkedList list = new LinkedList();
  
        // adding elements to check
        list.add(1);
        list.add(4);
        list.add(3);
        list.add(6);
        list.add(7);
        list.add(4);
        list.add(8);
  
        // printing the initial list
        System.out.println("The initial Linked List is : " + list);
  
        // computing result
        int res = list.lastIndexOf(4) - list.indexOf(4) - 1;
  
        // Printing the number of elements between 1st occurrence of 4 and last 4
        // prints 3
        System.out.println("The no. between 4s are :  " + res);
    }
}


Output :

The initial Linked List is : [Geeks, 4, Geeks, 8]
Index of 1st occurrence of Geeks : 0
Index of 1st occurrence of Astha : -1

2. lastIndexOf(Object o) : 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.

Declaration : 
  public int lastIndexOf(Object o)
Parameters : 
   o : element to search for
Return 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





Output :

The initial Linked List is : [Geeks, 4, Geeks, 8]
Index of last occurrence of Geeks : 2
Index of last occurrence of Astha : -1

Practical Application : Since both the functions display first and last index of a specific number or String etc, they can be useful to compute no. of elements, persons etc that are coming in between the last and 1st occurrence of value. Small example is given below.




// Java code to demonstrate the application
// of indexOf() in linked list
import java.util.*;
public class LinkedListIndexApp {
  
public static void main(String[] args)
    {
  
        // Declaring a LinkedList
        LinkedList list = new LinkedList();
  
        // adding elements to check
        list.add(1);
        list.add(4);
        list.add(3);
        list.add(6);
        list.add(7);
        list.add(4);
        list.add(8);
  
        // printing the initial list
        System.out.println("The initial Linked List is : " + list);
  
        // computing result
        int res = list.lastIndexOf(4) - list.indexOf(4) - 1;
  
        // Printing the number of elements between 1st occurrence of 4 and last 4
        // prints 3
        System.out.println("The no. between 4s are :  " + res);
    }
}


Output :

The initial Linked List is : [1, 4, 3, 6, 7, 4, 8]
The no. between 4s are :  3



Next Article

Similar Reads