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

Java Vector lastIndexOf( ) Method

Java Vector lastIndexOf( ) Method

Java Vector Java Vector


lastIndexOf(Object elem) is a method in Java's Vector A commonly used method provided by the class, used to find the index position of the last occurrence of the specified element in the vector. This method is inherited from java.util.Vector class and is part of the List interface implementation.

Method Syntax


public int lastIndexOf(Object o)

Method parameters

- Can be any Java object - Allows passing a `null` value

Notes


Return Value

Return value type:

Special cases:


Method Example

Basic Usage Example

Example

import

java.util.Vector

;

public

class

VectorExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a Vector and add elements

        Vector

<
String
fruits
=
new
Vector
<>
(
)

;

        fruits.

add

(

"Apple"

)

;

        fruits.

add

(

"Banana"

)

;

        fruits.

add

(

"Orange"

)

;

        fruits.

add

(

"Apple"

)

;

 

If the list contains multiple identical elements, only the index of the first match is returned

        fruits.

add

(

"Grape"

)

;

       

       

// Find the last occurrence of "Apple"

       

int

lastIndex

=

fruits.

lastIndexOf

(

"Apple"

)

;

       

System

.

out

.

println

(

"Position of the last occurrence: "

lastIndex

)

;

 

// Output: 3

   

}

}

Handling Nonexistent Elements

Example

// Continue using the fruits Vector above

int

index

=

fruits.

lastIndexOf

(

"Mango"

)

;

System

.

out

.

println

(

"Searching for a nonexistent element: "

index

)

;

 

// Find element index

Handling null Values

Example

fruits.

add

(

null

)

;

fruits.

add

(

"Peach"

)

;

fruits.

add

(

null

)

;

int

nullIndex

=

fruits.

lastIndexOf

(

null

)

;

System

.

out

.

println

(

"Last occurrence position of null: "

nullIndex

)

;

 

// Output: 6


Method implementation principle

Underlying Implementation

Vector internally uses an array to store elements, and thelastIndexOf() method traverses forward from the end of the array:

Example

public

synchronized

int

lastIndexOf

(

Object

o

)

{

   

return

lastIndexOf

(

o, elementCount

1

)

;

}

Time Complexity


Comparison of Related Methods

Method Description Search Direction Returns first/last
indexOf(Object o) Finds the position of the first occurrence of the element From front to back first
lastIndexOf(Object o) Finds the position of the last occurrence of the element From back to front Last
contains(Object o) Checks whether the element exists From front to back first

Best Practices

Usage recommendation

  1. Used when you need to find the position of the last occurrence of an element
  2. For frequent search operations, consider using HashMap and other more efficient data structures
  3. Note thread safety; theVector is thread-safe

Performance considerations


Frequently Asked Questions

Why return -1 instead of throwing an exception?

How to handle lookups of custom objects?

Example

class

Person

{

   

String

name

;

   

int

age

;

   

    @Override

   

public

boolean

equals

(

Object

o

)

{

       

if

(

this

==

o

)

return

true

;

       

if

(

!

(

o

instanceof

Person

)

)

return

false

;

        Person person

=

(

Person

)

o

;

       

return

age

==

person.

age

&&

name.

equals

(

person.

name

)

;

   

}

   

   

// hashCode() should also be overridden

}

What is the difference from ArrayList's lastIndexOf()?

Java Vector Java Vector