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
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短