在 Java 中,获取 List 中元素的索引主要有以下几种常用方法,适用于不同的场景和需求。

使用 indexOf() 方法(最常用)
这是最直接、最常用的方法,它会返回第一个匹配元素的索引,如果列表中不包含该元素,则返回 -1。
语法:
int indexOf(Object element)
示例:
import java.util.Arrays;
import java.util.List;
public class ListIndexOfExample {
public static void main(String[] args) {
List<String> fruits = Arrays.asList("Apple", "Banana", "Cherry", "Apple");
// 获取 "Cherry" 的索引
int cherryIndex = fruits.indexOf("Cherry");
System.out.println("索引 of 'Cherry': " + cherryIndex); // 输出: 2
// 获取第一个 "Apple" 的索引
int appleIndex = fruits.indexOf("Apple");
System.out.println("索引 of 'Apple' (第一个): " + appleIndex); // 输出: 0
// 获取一个不存在的元素的索引
int grapeIndex = fruits.indexOf("Grape");
System.out.println("索引 of 'Grape': " + grapeIndex); // 输出: -1
}
}
使用 lastIndexOf() 方法
如果你想获取最后一个匹配元素的索引,可以使用 lastIndexOf() 方法,如果列表中不包含该元素,同样返回 -1。

语法:
int lastIndexOf(Object element)
示例:
import java.util.Arrays;
import java.util.List;
public class ListLastIndexOfExample {
public static void main(String[] args) {
List<String> fruits = Arrays.asList("Apple", "Banana", "Cherry", "Apple");
// 获取最后一个 "Apple" 的索引
int lastAppleIndex = fruits.lastIndexOf("Apple");
System.out.println("最后一个 'Apple' 的索引: " + lastAppleIndex); // 输出: 3
// 获取 "Banana" 的索引(它只有一个)
int bananaIndex = fruits.lastIndexOf("Banana");
System.out.println("索引 of 'Banana': " + bananaIndex); // 输出: 1
// 获取一个不存在的元素的索引
int grapeIndex = fruits.lastIndexOf("Grape");
System.out.println("索引 of 'Grape': " + grapeIndex); // 输出: -1
}
}
使用传统的 for 循环(适用于复杂条件)
如果你需要在查找索引的同时执行更复杂的逻辑(查找满足特定条件的元素),或者需要获取所有匹配元素的索引,那么传统的 for 循环是最佳选择。
示例:
import java.util.ArrayList;
import java.util.List;
public class ListForLoopIndexExample {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>(List.of("Apple", "Banana", "Cherry", "Apple", "Date"));
// 查找所有 "Apple" 的索引
System.out.println("所有 'Apple' 的索引:");
for (int i = 0; i < fruits.size(); i++) {
if ("Apple".equals(fruits.get(i))) {
System.out.println(" - 索引 " + i);
}
}
// 输出:
// 所有 'Apple' 的索引:
// - 索引 0
// - 索引 3
// 查找长度大于 5 的第一个元素的索引
String longFruit = null;
int longFruitIndex = -1;
for (int i = 0; i < fruits.size(); i++) {
if (fruits.get(i).length() > 5) {
longFruit = fruits.get(i);
longFruitIndex = i;
break; // 找到第一个后立即退出循环
}
}
System.out.println("第一个长度大于 5 的元素是 '" + longFruit + "', 索引为: " + longFruitIndex);
// 输出: 第一个长度大于 5 的元素是 'Banana', 索引为: 1
}
}
使用 Java 8+ 的 Stream API(函数式风格)
对于现代 Java (8+) 开发,可以使用 Stream API 来查找索引,这种方式非常灵活,可以配合各种复杂的条件。
示例:
import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
import java.util.stream.IntStream;
public class ListStreamIndexExample {
public static void main(String[] args) {
List<String> fruits = Arrays.asList("Apple", "Banana", "Cherry", "Apple", "Date");
// 1. 查找第一个满足条件的元素的索引
// 使用 IntStream.range() 生成一个 0 到 size-1 的索引流
OptionalInt firstAppleIndex = IntStream.range(0, fruits.size())
.filter(i -> "Apple".equals(fruits.get(i)))
.findFirst();
firstAppleIndex.ifPresent(index -> System.out.println("第一个 'Apple' 的索引: " + index)); // 输出: 0
// 2. 查找所有满足条件的元素的索引
System.out.println("\n所有 'Apple' 的索引:");
IntStream.range(0, fruits.size())
.filter(i -> "Apple".equals(fruits.get(i)))
.forEach(System.out::println); // 输出: 0 和 3
// 3. 查找长度大于 5 的元素的索引
System.out.println("\n所有长度大于 5 的元素的索引:");
IntStream.range(0, fruits.size())
.filter(i -> fruits.get(i).length() > 5)
.forEach(System.out::println); // 输出: 1 和 2
}
}
注意: Stream API 返回的是 OptionalInt,因为它可能找不到任何匹配项(此时结果为空),这比直接返回 -1 更符合函数式编程的思想,可以避免 -1 这个“魔法数字”。
总结与对比
| 方法 | 描述 | 适用场景 | 返回值 |
|---|---|---|---|
indexOf() |
获取第一个匹配元素的索引。 | 简单、快速查找单个元素是否存在及其第一次出现的位置。 | int,找到则返回索引,否则返回 -1。 |
lastIndexOf() |
获取最后一个匹配元素的索引。 | 需要查找元素最后一次出现的位置。 | int,找到则返回索引,否则返回 -1。 |
for 循环 |
遍历列表,手动检查条件。 | 需要查找所有匹配项,或者在查找时执行复杂逻辑。 | 灵活,可以在循环中处理。 |
| Stream API | 使用函数式风格进行查找。 | Java 8+ 环境,需要复杂的、可组合的查找条件,或与流式操作结合。 | OptionalInt 或其他流式结果,更安全、更灵活。 |
如何选择?
- 简单查找第一个元素:直接用
list.indexOf(element),这是最简单、最高效的方式。 - 查找最后一个元素:使用
list.lastIndexOf(element)。 - 查找所有匹配元素或复杂条件:使用
for循环或 Stream API,Stream API 在代码简洁性和可读性上更优,尤其是在复杂查询时。
