Java Vector lastIndexOf( ) Method
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
Object o: The element to find in the vector- Can be any Java object
- Allows passing in
nullValue
Notes
- Element comparison uses
equals()Method - If the vector contains multiple equal elements, returns the index of the last match
Return Value
Return value type:
int: The index position of the last occurrence of the element
Special cases:
- If the element does not exist in the vector, returns
-1 - If the vector is empty, returns for any search
-1
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
- The worst case is O(n), where n is the size of the vector
- Needs to traverse the entire vector to find the element
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
- Used when you need to find the position of the last occurrence of an element
- For frequent search operations, consider using
HashMapand other more efficient data structures - Note thread safety; the
Vectoris thread-safe
Performance considerations
- For large
Vector; frequent calls to this method may affect performance - If you only need to know whether the element exists, using
contains()method is more efficient
Frequently Asked Questions
Why return -1 instead of throwing an exception?
- This is a conventional practice that allows the caller to handle the case where the element does not exist uniformly
- The difference between
indexOf()method consistent
How to handle lookups of custom objects?
- Ensure the custom class correctly implements
equals()Method - Example:
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()?
- The functionality is exactly the same
- The main difference is that the
Vectormethod is synchronized (thread-safe) ArrayListversion has slightly higher performance (not synchronized)
Java Vector