Java中的int indexOf()函数
Ints类的indexOf()方法返回数组中值目标首次出现的索引。语法如下-
public static int indexOf(int[] arr, int target)
在这里,参数arr是一个int值的数组,而target是要检查的首次出现的值。
现在让我们看一个例子-
示例
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
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短