Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java LinkedList indexOf() Method · Private Archive

Java LinkedList indexOf() Method · Private Archive

Java LinkedList Java LinkedList


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:

Parameter Description

Parameters Type Description
o Object If the list does not contain the element, it returns -1

Notes

  1. The element to be found in the list null
  2. The parameter can be any object type, including equals() Uses
  3. 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

  1. method finds elements by traversing the list nodes:
  2. Start traversing from the first node (head node) of the list equals() For each node, use
  3. method to compare elements
  4. When the first matching node is found, return the current index

Time Complexity

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

  1. Performance considerations// ... Add data that may have duplicatesArrayList 's indexOf() : For frequent lookup operations, LinkedList is usually faster than
  2. fasterElement comparison LinkedList : Make sure the objects stored in equals() Method
  3. Concurrent Modificationcorrectly implement ConcurrentModificationException
  4. : If the list is modified during iteration, it may throwNull handling null : You can find NullPointerException elements, but be careful about

Summary

LinkedList 's indexOf() the risk

Applicable to various object types, including

Java LinkedList Java LinkedList