Java 中的 Ints lastIndexOf() 函数
java 8object oriented programmingprogramming更新于 2025/12/4 7:40:17
Ints 类的 lastIndexOf() 方法返回目标值在数组中最后一次出现的索引。其语法如下:−
public static int lastIndexOf(int[] arr, int target)
其中,arr 是 int 值数组,而 target 是我们要查找其最后一个索引的 int 值。
我们先来看一个例子:−
示例
import com.google.common.primitives.Ints;
class Demo {
public static void main(String[] args) {
int[] myArr = { 10, 20, 30, 40, 50, 60,20, 80, 20, 100 };
System.out.println(Ints.join("-", myArr));
// 查找元素 20 的最后一个索引
int index = Ints.lastIndexOf(myArr, 20);
if (index != -1) {
System.out.println("The last index of element 20 = " + index);
} else {
System.out.println("The element 20 is not in the array.");
}
}
}
输出
10-20-30-40-50-60-20-80-20-100 The last index of element 20 = 8

