The Java.util.LinkedList.indexOf(Object element) method is used to check and find the occurrence of a particular element in the list. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the list does not contain the element.
Syntax:
LinkedList.indexOf(Object element)
Parameters: The parameter element is of the type LinkedList. It specifies the element whose occurrence is needed to be checked in the LinkedList.
Return Value: The method returns the index or position of the first occurrence of the element in the list else -1 if the element is not present in the list. The returned value is of integer type.
Below program illustrate the Java.util.LinkedList.indexOf() method:
import java.io.*;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String args[]) {
LinkedList<String> list = new LinkedList<String>();
list.add( "Geeks" );
list.add( "for" );
list.add( "Geeks" );
list.add( "10" );
list.add( "20" );
System.out.println( "LinkedList:" + list);
System.out.println( "The first occurrence of Geeks is at index:"
+ list.indexOf( "Geeks" ));
System.out.println( "The first occurrence of 10 is at index: "
+ list.indexOf( "10" ));
}
}
|
Output:
LinkedList:[Geeks, for, Geeks, 10, 20]
The first occurrence of Geeks is at index: 0
The first occurrence of 10 is at index: 3
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!