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.
import java.util.*;
public class LinkedListIndexOf {
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.add( "Geeks" );
list.add( 4 );
list.add( "Geeks" );
list.add( 8 );
System.out.println( "The initial Linked List is : " + list);
System.out.println( "Index of 1st occurrence of Geeks : "
+ list.indexOf( "Geeks" ));
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
import java.util.*;
public class LinkedListLastindexOf {
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.add( "Geeks" );
list.add( 4 );
list.add( "Geeks" );
list.add( 8 );
System.out.println( "The initial Linked List is : " + list);
System.out.println( "Index of last occurrence of Geeks : "
+ list.lastIndexOf( "Geeks" ));
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.
import java.util.*;
public class LinkedListIndexApp {
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.add( 1 );
list.add( 4 );
list.add( 3 );
list.add( 6 );
list.add( 7 );
list.add( 4 );
list.add( 8 );
System.out.println( "The initial Linked List is : " + list);
int res = list.lastIndexOf( 4 ) - list.indexOf( 4 ) - 1 ;
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
Last Updated :
06 Jan, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...