LinkedList indexOf() method in Java
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:
// Java code to illustrate indexOf() import java.io.*; import java.util.LinkedList; public class LinkedListDemo { public static void main(String args[]) { // Creating an empty LinkedList LinkedList<String> list = new LinkedList<String>(); // Use add() method to add elements in the list list.add("Geeks"); list.add("for"); list.add("Geeks"); list.add("10"); list.add("20"); // Displaying the list System.out.println("LinkedList:" + list); // The first position of an element // is returned 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")); } } |
LinkedList:[Geeks, for, Geeks, 10, 20] The first occurrence of Geeks is at index: 0 The first occurrence of 10 is at index: 3
Recommended Posts:
- Java.util.LinkedList.indexOf(), lastIndexof() in Java
- CopyOnWriteArrayList indexOf() method in Java
- Stack indexOf() method in Java with Example
- AbstractSequentialList indexOf() method in Java with Example
- Vector indexOf() Method in Java
- StringBuffer indexOf() method in Java with Examples
- List indexOf() Method in Java with Examples
- Stack indexOf(Object, int) method in Java with Example
- AbstractList indexOf() method in Java with Examples
- StringBuilder indexOf() method in Java with Examples
- LinkedList contains() Method in Java
- LinkedList add() Method in Java
- LinkedList get() Method in Java
- LinkedList set() Method in Java
- LinkedList pop() Method in Java
If you like GeeksforGeeks 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 GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



