Java Vector lastIndexOf(Object elem, int index) 方法
lastIndexOf(Object elem, int index)
是 Java 中 Vector
类提供的一个重要方法,用于在指定范围内从后向前搜索指定元素。这个方法可以帮助开发者高效地查找集合中特定元素的位置。
方法语法:
public int lastIndexOf(Object o, int index)
参数说明
Object o
- 需要在 Vector 中查找的元素- 可以是任何类型的对象
- 如果 Vector 中包含 null 元素,也可以搜索 null
int index
- 搜索的起始索引位置- 搜索从这个索引开始向前进行(向 Vector 的开头方向)
- 这个索引是包含在搜索范围内的
注意事项
- 如果传入的
index
大于或等于 Vector 的当前大小,整个 Vector 都会被搜索 - 如果
index
为负数,会抛出IndexOutOfBoundsException
返回值
返回值类型
- 返回
int
类型的值
返回值含义
- 如果找到元素:返回元素在 Vector 中的最后一个索引位置(从指定位置向前搜索)
- 如果未找到元素:返回 -1
- 如果参数
index
超出范围:抛出IndexOutOfBoundsException
方法示例
基本使用示例
实例
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
// 创建一个 Vector 并添加元素
Vector<String> fruits = new Vector<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple");
fruits.add("Grape");
// 从索引 3 开始向前搜索 "Apple"
int index = fruits.lastIndexOf("Apple", 3);
System.out.println("Last index of 'Apple' from index 3: " + index);
// 从索引 2 开始向前搜索 "Banana"
index = fruits.lastIndexOf("Banana", 2);
System.out.println("Last index of 'Banana' from index 2: " + index);
// 搜索不存在的元素
index = fruits.lastIndexOf("Peach", 4);
System.out.println("Last index of 'Peach' from index 4: " + index);
}
}
public class VectorExample {
public static void main(String[] args) {
// 创建一个 Vector 并添加元素
Vector<String> fruits = new Vector<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple");
fruits.add("Grape");
// 从索引 3 开始向前搜索 "Apple"
int index = fruits.lastIndexOf("Apple", 3);
System.out.println("Last index of 'Apple' from index 3: " + index);
// 从索引 2 开始向前搜索 "Banana"
index = fruits.lastIndexOf("Banana", 2);
System.out.println("Last index of 'Banana' from index 2: " + index);
// 搜索不存在的元素
index = fruits.lastIndexOf("Peach", 4);
System.out.println("Last index of 'Peach' from index 4: " + index);
}
}
输出结果
Last index of 'Apple' from index 3: 3 Last index of 'Banana' from index 2: 1 Last index of 'Peach' from index 4: -1
方法实现原理
底层实现
Vector
类内部使用数组来存储元素,lastIndexOf(Object o, int index)
方法的实现本质上是从指定的索引位置开始,向前遍历数组,直到找到匹配的元素或到达数组开头。
时间复杂度
- 最坏情况下时间复杂度为 O(n),其中 n 是从起始索引到 Vector 开头的元素数量
- 平均情况下时间复杂度也是 O(n)
与类似方法的比较
与
lastIndexOf(Object o)
的区别:lastIndexOf(Object o)
搜索整个 VectorlastIndexOf(Object o, int index)
只搜索从指定索引到 Vector 开头的部分
与
indexOf(Object o, int index)
的区别:indexOf
是从指定位置向后搜索lastIndexOf
是从指定位置向前搜索
使用场景
适用情况
- 当需要在 Vector 的某个特定范围内查找元素的最后一次出现位置时
- 当需要从某个已知位置向前搜索时
- 当处理大型 Vector 且知道目标元素位于某个区域时,可以提高搜索效率
实际应用示例
实例
// 在日志分析中查找特定错误信息的最后一次出现
Vector<String> logEntries = getLogEntries();
int lastErrorIndex = logEntries.lastIndexOf("ERROR", logEntries.size() - 1);
// 在游戏开发中查找玩家最近的位置记录
Vector<PlayerPosition> positionHistory = getPositionHistory();
int lastCheckpointIndex = positionHistory.lastIndexOf(
new PlayerPosition(checkpointX, checkpointY),
currentPositionIndex
);
Vector<String> logEntries = getLogEntries();
int lastErrorIndex = logEntries.lastIndexOf("ERROR", logEntries.size() - 1);
// 在游戏开发中查找玩家最近的位置记录
Vector<PlayerPosition> positionHistory = getPositionHistory();
int lastCheckpointIndex = positionHistory.lastIndexOf(
new PlayerPosition(checkpointX, checkpointY),
currentPositionIndex
);
注意事项
边界条件处理
空值处理:
- Vector 可以包含 null 元素
- 可以搜索 null:
lastIndexOf(null, index)
索引越界:
- 如果 index >= size(),会搜索整个 Vector
- 如果 index < 0,会抛出 IndexOutOfBoundsException
性能考虑
- 对于大型 Vector,频繁使用此方法可能影响性能
- 如果需要频繁搜索,考虑使用其他数据结构如 HashMap
线程安全
- Vector 是线程安全的
- 在多线程环境下使用此方法不需要额外同步
- 但要注意如果在搜索过程中 Vector 被修改,可能会影响搜索结果
总结
Vector.lastIndexOf(Object elem, int index)
是一个实用的方法,它提供了从指定位置向前搜索元素的能力。理解这个方法的工作原理和适用场景,可以帮助开发者更高效地处理集合搜索需求。记住合理处理边界条件,并在性能敏感的场景中考虑替代方案。
点我分享笔记