Java Vector lastIndexOf( ) Method
lastIndexOf(Object elem)is a commonly used method in JavaVectora common method provided by the class, used to find the index of the last occurrence of the specified element in the vector. This method inherits fromjava.util.Vectorthe AbstractList class, and is part of the List interface implementation.
Method Syntax
public int lastIndexOf(Object o)
Method Parameters
Object o: the element to search for in the vector- can be any Java object
- allows passing
nullValue
Notes
- Element comparison uses the
equals()method - If the vector contains multiple equal elements, returns the index of the last matching one.
Return Value
Return type:
int: the index 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, any search will return
-1
Method Examples
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"); // Duplicate element
fruits.add("Grape");
// Find the last occurrence of "Apple"
int lastIndex = fruits.lastIndexOf("Apple");
System.out.println("Last occurrence position: " + lastIndex); // Output: 3
}
}
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"); // Duplicate element
fruits.add("Grape");
// Find the last occurrence of "Apple"
int lastIndex = fruits.lastIndexOf("Apple");
System.out.println("Last occurrence position: " + lastIndex); // Output: 3
}
}
Handling Non-existent Elements
Example
// Continue using the fruits Vector above
int index = fruits.lastIndexOf("Mango");
System.out.println("Searching for a non-existent element: " + index); // Output: -1
int index = fruits.lastIndexOf("Mango");
System.out.println("Searching for a non-existent element: " + index); // Output: -1
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
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
VectorInternally uses an array to store elements,lastIndexOf()the method traverses from the end of the array backward to search:
Example
public synchronized int lastIndexOf(Object o) {
return lastIndexOf(o, elementCount-1);
}
return lastIndexOf(o, elementCount-1);
}
Time Complexity
- In the worst case O(n), where n is the size of the vector.
- It needs to traverse the entire vector to find the element.
Comparison of Related Methods
| Method | Description | Search Direction | Return First/Last |
|---|---|---|---|
indexOf(Object o) |
Find the first occurrence of the element | From front to back | First |
lastIndexOf(Object o) |
Find the last occurrence of the element | From back to front | Last |
contains(Object o) |
Check whether the element exists | From front to back | First |
Best Practices
Usage Recommendations
- Use it when you need to find the last occurrence of an element.
- For frequent search operations, consider using
HashMapand other more efficient data structures - Note thread safety:
Vectorit is thread-safe
Performance Considerations
- For large
Vector, frequent calls to this method may affect performance - If you only need to know whether an element exists, use
contains()method is more efficient
Frequently Asked Questions
Why return -1 instead of throwing an exception?
- This is a conventional practice, allowing callers to uniformly handle the case where the element does not exist.
- and
indexOf()consistent with the method
How to handle searching for custom objects?
- Ensure that the custom class correctly implements the
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
}
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()?
- Functionally identical
- The main difference is
Vectorthat the method is synchronized (thread-safe) ArrayListthe version has slightly higher performance (non-synchronized)
Java Vector