Java中的int lastIndexOf()函数
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("元素 20 的最后一个索引 = " + index);
} else {
System.out.println("元素20不在数组中.");
}
}
}输出结果
10-20-30-40-50-60-20-80-20-100 元素 20 的最后一个索引 = 8
热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬