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

Java LinkedList lastIndexOf() Method · Private Archive

Detailed explanation of the Java LinkedList lastIndexOf() method

Java LinkedList Java LinkedList


lastIndexOf() The method is in Java's LinkedList is a practical method provided by the class for finding the index of the last occurrence of a specified element in the list. If the list does not contain the element, it returns -1.

This method is inherited from List interface, in LinkedList implementation, it searches for elements forward from the tail of the list.

Method Syntax



public int lastIndexOf(Object o)

Parameter Description


Usage examples

Let us understand through a few examples lastIndexOf() the use of the method:

Example 1: Basic usage

Example

import

java.util.LinkedList

;

public

class

LastIndexOfExample

{

   

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"

)

;

        fruits.

add

(

"Mango"

)

;

       

       

// Find the last occurrence position of "Banana"

       

int

lastIndex

=

fruits.

lastIndexOf

(

"Banana"

)

;

       

System

.

out

.

println

(

"Last occurrence position of Banana: "

lastIndex

)

;

       

       

// Find a non-existent element

       

int

notFound

=

fruits.

lastIndexOf

(

"Grape"

)

;

       

System

.

out

.

println

(

"Position of Grape: "

notFound

)

;

   

}

}

Output result:

Banana 最后一次出现的位置: 3

Grape 的位置: -1

Example 2: Handling null elements

Example

import

java.util.LinkedList

;

public

class

NullExample

{

   

public

static

void

main

(

String

[

]

args

)

{

        LinkedList

<

String

list

=

new

LinkedList

<>

(

)

;

        list.

add

(

"A"

)

;

        list.

add

(

null

)

;

        list.

add

(

"B"

)

;

        list.

add

(

null

)

;

       

       

int

index

=

list.

lastIndexOf

(

null

)

;

       

System

.

out

.

println

(

"Last occurrence position of null: "

index

)

;

   

}

}

Output result:

null 最后一次出现的位置: 3


Method implementation principle

LinkedList The lastIndexOf() The method implementation logic is as follows:

  1. Traverse forward starting from the tail of the list
  2. For each element, use equals() method to compare with the target element
  3. After finding the first matching element, return its index
  4. If no matching element is found after traversing the entire list, return -1

Performance considerations


Frequently Asked Questions

Q1: What is the difference between lastIndexOf() and indexOf()?

Q2: Is this method case-sensitive?

Q3: Can it be used for custom objects?


Practical Application Scenarios

lastIndexOf() The method is especially useful in the following scenarios:

  1. Log analysis: Find the last occurrence position of a certain log message
  2. History records: To find the last occurrence of a specific operation in the user operation history
  3. Data cleaning: To locate the last occurrence of duplicate data for processing

Summary

LinkedList 's lastIndexOf() method is a simple but practical tool that helps us quickly locate the last position of an element in a list. Understanding how this method works and its applicable scenarios enables us to handle linked list data more efficiently in development.

Java LinkedList Java LinkedList