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
热门推荐
7 修祖屋祝福语简短
8 带清的简短祝福语
10 祝贺开业文案祝福语简短
11 周年蛋糕祝福语简短英语
12 最搞笑生日祝福语简短
13 娶嫂子的祝福语简短
14 八一幼儿祝福语大全简短
15 怀念青春祝福语简短
16 农民朋友发财祝福语简短
17 祝老婆生日祝福语简短
18 师姐毕业送花祝福语简短