Ints 的 lastIndexOf() 函数在 Java 中
Ints 类的 lastIndexOf() 方法返回数组中 target 值最后出现时的索引。语法如下 −
public static int lastIndexOf(int[] arr, int target)
其中,arr 是整数值数组,target 是我们正在查找其最后索引的整数值。
我们先来看一个例子 −
示例
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));
// finding last index of element 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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP