Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaJava.util.LinkedList.indexOf(), lastIndexof() in Java

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

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 working
// of indexOf(Object o) in linked list
import java.util.*;
public class LinkedListIndexOf {
  
public static void main(String[] args)
    {
  
        // Declaring a LinkedList
        LinkedList list = new LinkedList();
  
        // adding elements
        list.add("Geeks");
        list.add(4);
        list.add("Geeks");
        list.add(8);
  
        // printing the initial list
        System.out.println("The initial Linked List is : " + list);
  
        // Retrieving index of 1st occurrence of "Geeks"
        // Prints 0
        System.out.println("Index of 1st occurrence of Geeks : "
             + list.indexOf("Geeks"));
  
        // Retrieving index of 1st occurrence of "Astha"
        // Prints -1
        // element not present
        System.out.println("Index of 1st occurrence of Astha : "
             + list.indexOf("Astha"));
    }
}


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




// Java code to demonstrate the working
// of lastIndexOf(Object o) in linked list
import java.util.*;
public class LinkedListLastindexOf {
public static void main(String[] args)
    {
        // Declaring a LinkedList
        LinkedList list = new LinkedList();
        // adding elements
        list.add("Geeks");
        list.add(4);
        list.add("Geeks");
        list.add(8);
        // printing the initial list
        System.out.println("The initial Linked List is : " + list);
        // Retrieving index of last occurrence of "Geeks"
        // Prints 2
        System.out.println("Index of last occurrence of Geeks : " 
            + list.lastIndexOf("Geeks"));
  
        // Retrieving index of last occurrence of "Astha"
        // Prints -1
        // element not present
        System.out.println("Index of last occurrence of Astha : " 
            + list.lastIndexOf("Astha"));
    }
}


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

This article is contributed by Astha Tyagi. If you like Lazyroar 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 Lazyroar main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

RELATED ARTICLES

Most Popular

Recent Comments