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
热门推荐
9 十月简短祝福语
10 参加儿子大学祝福语简短
11 比较简短浪漫的祝福语
12 下雨开工吉祥祝福语简短
13 怀念青春祝福语简短
14 职场祝福语大全 简短精辟
15 近代生日祝福语简短精辟
16 出阁祝福语简短句子
17 送外卖祝福语大全简短
18 送灯祝福语简短独特