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 送给学长毕业祝福语简短