Java LinkedList indexOf() Method · Private Archive
indexOf() The method is in Java's LinkedList Java LinkedList indexOf() Method java.util.LinkedList class, inherited from java.util.AbstractList class.
Method Syntax:
public int indexOf(Object o)
Return Value:
- is a commonly used method provided by the class for finding the first occurrence position of a specified element in the list. This method belongs to
- Returns the index of the first occurrence of the specified element in the list (counting from 0)
Parameter Description
| Parameters | Type | Description |
|---|---|---|
| o | Object | If the list does not contain the element, it returns -1 |
Notes
- The element to be found in the list
null - The parameter can be any object type, including
equals()Uses - method for element comparison
Usage examples
Basic example
Example
import
java.util.LinkedList
;
public
class
LinkedListIndexOfExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create a LinkedList
LinkedList
<
String
fruits
=
new
LinkedList
<>
(
);
// Add elements
fruits.
add
(
"Apple"
)
;
fruits.
add
(
"Banana"
)
;
fruits.
add
(
"Orange"
)
;
fruits.
add
(
"Banana"
)
;
If the list contains multiple identical elements, only the index of the first match is returned
// Duplicate elements
System
.
out
.
println
(
"Index of 'Banana': "
fruits.
indexOf
(
"Banana"
)
)
;
// Output: 1
System
.
out
.
println
(
"Index of 'Grape': "
fruits.
indexOf
(
"Grape"
)
)
;
// Find element index
System
.
out
.
println
(
"Index of 'Orange': "
fruits.
indexOf
(
"Orange"
)
)
;
// Output: 2
}
}
// Output: -1
Example
import
java.util.LinkedList
;
public
class
NullIndexOfExample
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
String
list
=
new
LinkedList
<>
(
);
list.
add
(
"A"
)
;
list.
add
(
null
)
;
list.
add
(
"B"
)
;
System
.
out
.
println
(
"Index of null: "
list.
indexOf
(
null
)
)
;
// Output: 1
}
}
Implementation principle
Underlying Implementation
LinkedList 's indexOf() Handling null elements
- method finds elements by traversing the list nodes:
- Start traversing from the first node (head node) of the list
equals()For each node, use - method to compare elements
- When the first matching node is found, return the current index
Time Complexity
- If no match is found after traversing all nodes, return -1
- Average time complexity: O(n)
Because LinkedList Worst-case time complexity: O(n)
Comparison with Related Methods
indexOf() vs lastIndexOf()
| Method | Description | is implemented based on a linked list and needs to start sequential search from the head; it cannot randomly access elements like an array. |
|---|---|---|
| indexOf() | Search direction | Returns the position of the first occurrence of the element |
| lastIndexOf() | From head to tail | Returns the position of the last occurrence of the element |
indexOf() vs contains()
| Method | Return Value | Purpose |
|---|---|---|
| indexOf() | From tail to head | int (index position) |
| contains() | boolean | Use when you need to know the element position |
Use when you only need to know whether the element existscontains() In fact, indexOf() >= 0 method internally calls
Practical Application Scenarios
to implement it.
Example
LinkedList
<
String
usernames
=
new
LinkedList
<>
(
);
Scenario 1: Check whether an element exists
String
input
=
"admin"
;
if
(
usernames.
indexOf
(
input
)
!=
1
)
{
System
.
out
.
println
(
// ... Add usernames
)
;
}
else
{
System
.
out
.
println
(
"Username already exists"
)
;
}
"Can register"
Example
LinkedList
<
Task
taskList
=
new
LinkedList
<>
(
);
Scenario 2: Get the element position for subsequent operations
int
index
=
taskList.
indexOf
(
importantTask
)
;
if
(
index
!=
1
)
{
// ... Add tasks
taskList.
add
(
index, newUrgentTask
)
;
}
// Insert a new task before an important task
Example
LinkedList
<
String
listWithDuplicates
=
new
LinkedList
<>
(
);
Scenario 3: Data deduplication
LinkedList
<
String
uniqueList
=
new
LinkedList
<>
(
);
for
(
String
item
:
listWithDuplicates
)
{
if
(
uniqueList.
indexOf
(
item
)
==
1
)
{
uniqueList.
add
(
item
)
;
}
}
Notes
- Performance considerations// ... Add data that may have duplicates
ArrayList'sindexOf(): For frequent lookup operations,LinkedListis usually faster than - fasterElement comparison
LinkedList: Make sure the objects stored inequals()Method - Concurrent Modificationcorrectly implement
ConcurrentModificationException - : If the list is modified during iteration, it may throwNull handling
null: You can findNullPointerExceptionelements, but be careful about
Summary
LinkedList 's indexOf() the risk
- method is a practical lookup tool that:
- Is simple and easy to use; just pass in the element to find
- Returns the index of the first occurrence of the element
- Returns -1 when not found
null
Applicable to various object types, including
Java LinkedList