Java Vector lastIndexOf(Object elem, int index) Method
lastIndexOf(Object elem, int index)isVectoran important method provided by the class in Java, used to search backward for the specified element within a specified range. This method helps developers efficiently find the position of a specific element in a collection.
Method syntax:
public int lastIndexOf(Object o, int index)
Parameter Description
Object o- The element to be found in the Vector- Can be an object of any type
- If the Vector contains null elements, null can also be searched
int index- The starting index position of the search- The search proceeds forward from this index (toward the beginning of the Vector)
- This index is included in the search range
Notes
- If the passed
indexis greater than or equal to the current size of the Vector, the entire Vector will be searched. - If
indexis negative, a ... will be thrown.IndexOutOfBoundsException
Return Value
Return Value Type
- Returns
inta value of type
Meaning of the Return Value
- If the element is found: returns the last index position of the element in the Vector (searching forward from the specified position)
- If the element is not found: returns -1
- If the parameter
indexis out of range: throwsIndexOutOfBoundsException
Method Example
Basic Usage Example
Example
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");
fruits.add("Grape");
// Search forward for "Apple" starting from index 3
int index = fruits.lastIndexOf("Apple", 3);
System.out.println("Last index of 'Apple' from index 3: " + index);
// Search forward for "Banana" starting from index 2
index = fruits.lastIndexOf("Banana", 2);
System.out.println("Last index of 'Banana' from index 2: " + index);
// Search for a non-existent element
index = fruits.lastIndexOf("Peach", 4);
System.out.println("Last index of 'Peach' from index 4: " + index);
}
}
Output Result
Last index of 'Apple' from index 3: 3 Last index of 'Banana' from index 2: 1 Last index of 'Peach' from index 4: -1
Implementation Principle of the Method
Underlying Implementation
VectorThe class internally uses an array to store elements.lastIndexOf(Object o, int index)The implementation of the method essentially starts from the specified index position and traverses the array forward until it finds a matching element or reaches the beginning of the array.
Time Complexity
- The worst-case time complexity is O(n), where n is the number of elements from the starting index to the beginning of the Vector.
- The average-case time complexity is also O(n).
Comparison with Similar Methods
and
lastIndexOf(Object o)The difference:lastIndexOf(Object o)Searches the entire VectorlastIndexOf(Object o, int index)Only searches the part from the specified index to the beginning of the Vector
and
indexOf(Object o, int index)The difference:indexOfSearches backward from the specified positionlastIndexOfSearches forward from the specified position
Usage Scenarios
Applicable Cases
- When you need to find the last occurrence position of an element within a specific range of the Vector
- When you need to search forward from a known position
- When dealing with a large Vector and knowing the target element is in a certain area, it can improve search efficiency
Practical Application Example
Example
Vector<String> logEntries = getLogEntries();
int lastErrorIndex = logEntries.lastIndexOf("ERROR", logEntries.size() - 1);
// Find the player's most recent location record in game development
Vector<PlayerPosition> positionHistory = getPositionHistory();
int lastCheckpointIndex = positionHistory.lastIndexOf(
new PlayerPosition(checkpointX, checkpointY),
currentPositionIndex
);
Notes
Boundary Condition Handling
Null Value Handling:
- A Vector can contain null elements
- Null can be searched:
lastIndexOf(null, index)
Index Out of Bounds:
- If index >= size(), the entire Vector will be searched
- If index < 0, an IndexOutOfBoundsException will be thrown
Performance Considerations
- For large Vectors, frequent use of this method may affect performance
- If frequent searches are needed, consider using other data structures such as HashMap
Thread Safety
- Vector is thread-safe
- Using this method in a multi-threaded environment does not require additional synchronization
- But note that if the Vector is modified during the search, the search results may be affected
Summary
Vector.lastIndexOf(Object elem, int index)This is a practical method that provides the ability to search for elements forward from a specified position. Understanding how this method works and its applicable scenarios can help developers handle collection search needs more efficiently. Remember to properly handle boundary conditions and consider alternatives in performance-sensitive scenarios.
Java Vector