Java Linkedlist Indexof
[ Java LinkedList](#)\\n\\n* * *\\n\\nThe `indexOf()` method is a commonly used method provided by the `LinkedList` class in Java, used to find the first occurrence position of a specified element in the linked list. This method belongs to the `java.util.LinkedList` class and inherits from the `java.util.AbstractList` class.\\n\\n**Method Syntax**:\\n\\npublic int indexOf(Object o)\\n**Return Value**:\\n\\n* Returns the index of the first occurrence of the specified element in the linked list (counting starts from 0)\\n* Returns -1 if the linked list does not contain the element\\n\\n### Parameter Description\\n\\n| Parameter | Type | Description |\\n| --- | --- | --- |\\n| o | Object | The element to search for in the linked list |\\n\\n### Notes\\n\\n1. The parameter can be any object type, including `null`\\n2. The `equals()` method is used for element comparison during the search\\n3. If the linked list contains multiple identical elements, only the index of the first matching item is returned\\n\\n* * *\\n\\n## Usage Examples\\n\\n### Basic Example\\n\\n## Example\\n\\nimport java.util.LinkedList;\\n\\npublic class LinkedListIndexOfExample {\\n\\npublic static void main(String[] args){\\n\\n// Create a LinkedList\\n\\n LinkedList fruits =new LinkedList();\\n\\n// Add element\\n\\n fruits.add("Apple");\\n\\n fruits.add("Banana");\\n\\n fruits.add("Orange");\\n\\n fruits.add("Banana");// Duplicate elements\\n\\n// Find element index\\n\\nSystem.out.println("Index of 'Banana': "+ fruits.indexOf("Banana"));// Output: 1\\n\\nSystem.out.println("Index of 'Grape': "+ fruits.indexOf("Grape"));// Output: -1\\n\\nSystem.out.println("Index of 'Orange': "+ fruits.indexOf("Orange"));// Output: 2\\n\\n}\\n\\n}\\n\\n### Handling null Elements\\n\\n## Example\\n\\nimport java.util.LinkedList;\\n\\npublic class NullIndexOfExample {\\n\\npublic static void main(String[] args){\\n\\n LinkedList list =new LinkedList();\\n\\n list.add("A");\\n\\n list.add(null);\\n\\n list.add("B");\\n\\nSystem.out.println("Index of null: "+ list.indexOf(null));// Output: 1\\n\\n}\\n\\n}\\n\\n* * *\\n\\n## Implementation Principle\\n\\n### Underlying Implementation\\n\\nThe `indexOf()` method of `LinkedList` finds elements by traversing linked list nodes:\\n\\n1. Start traversing from the first node (head node) of the linked list\\n2. For each node, use the `equals()` method to compare elements\\n3. When the first matching node is found, return the current index\\n4. If no match is found after traversing all nodes, return -1\\n\\n### Time Complexity\\n\\n* Average time complexity: O(n)\\n* Worst-case time complexity: O(n)\\n\\nSince `LinkedList` is implemented based on a linked list, it needs to search sequentially from the beginning and cannot access elements randomly like arrays.\\n\\n* * *\\n\\n## Comparison with Related Methods\\n\\n### indexOf() vs lastIndexOf()\\n\\n| Method | Description | Search Direction |\\n| --- | --- | --- |\\n| indexOf() | Returns the position of the first occurrence of an element | From head to tail |\\n| lastIndexOf() | Returns the position of the last occurrence of an element | From tail to head |\\n\\n### indexOf() vs contains()\\n\\n| Method | Return Value | Usage |\\n| --- | --- | --- |\\n| indexOf() | int (index position) | Used when you need to know the element's position |\\n| contains() | boolean | Used when you only need to know if the element exists |\\n\\nIn fact, the `contains()` method is internally implemented by calling `indexOf() >= 0`.\\n\\n* * *\\n\\n## Practical Application Scenarios\\n\\n### Scenario 1: Checking if an Element Exists\\n\\n## Example\\n\\nLinkedList usernames =new LinkedList();\\n\\n// ... Add username\\n\\nString input ="admin";\\n\\nif(usernames.indexOf(input)!=-1){\\n\\nSystem.out.println("Username already exists");\\n\\n}else{\\n\\nSystem.out.println("Can register");\\n\\n}\\n\\n### Scenario 2: Getting Element Position for Subsequent Operations\\n\\n## Example\\n\\nLinkedList taskList =new LinkedList();\\n\\n// ... Add task\\n\\nint index = taskList.indexOf(importantTask);\\n\\nif(index !=-1){\\n\\n// Insert new task before important task\\n\\n taskList.add(index, newUrgentTask);\\n\\n}\\n\\n### Scenario 3: Data Deduplication\\n\\n## Example\\n\\nLinkedList listWithDuplicates =new LinkedList();\\n\\n// ... Add potentially duplicate data\\n\\nLinkedList uniqueList =new LinkedList();\\n\\nfor(String item : listWithDuplicates){\\n\\nif(uniqueList.indexOf(item)==-1){\\n\\n uniqueList.add(item);\\n\\n}\\n\\n}\\n\\n* * *\\n\\n## Notes\\n\\n1. **Performance Consideration**: For frequent search operations, `ArrayList`'s `indexOf()` is usually faster than `LinkedList`\\n2. **Element Comparison**: Ensure that objects stored in `LinkedList` correctly implement the `equals()` method\\n3. **Concurrent Modification**: If the linked list is modified during iteration, a `ConcurrentModificationException` may be thrown\\n4. **null Handling**: You can search for `null` elements, but be aware of the `NullPointerException` risk\\n\\n* * *\\n\\n## Summary\\n\\nThe `indexOf()` method of `LinkedList` is a practical search tool that:\\n\\n* Is simple and easy to use, just pass in the element to search for\\n* Returns the index position of the first occurrence of the element\\n* Returns -1 when the element is not found\\n* Applicable to various object types, including `null`\\n\\nUnderstanding and proficiently using this method can help you more effectively handle data search requirements in linked lists.\\n\\n[ Java LinkedList](#)
YouTip