Ints indexOf() 函数在 Java 中
Ints 类的 indexOf() 函数返回数组 target 中 value 首次出现的索引。语法如下 -
public static int indexOf(int[] arr, int target)
其中,参数 arr 是 int 值的数组,target 是要检查其首次出现的 value。
现在让我们看一个示例 -
示例
import com.google.common.primitives.Ints;
class Demo {
public static void main(String[] args) {
int[] arr = { 10, 20, 30, 40, 50, 60,70, 80, 90, 100 };
int index = Ints.indexOf(arr, 40);
if (index != -1) {
System.out.println("Element 40 is in the array at position " + (index+1));
} else {
System.out.println("Element 40 is not in the array");
}
}
}输出
Element 40 is in the array at position 4
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP