Java 中的 Ints indexOf() 函数
java 8object oriented programmingprogramming更新于 2025/6/26 15:07:17
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("元素 40 在数组中的位置 &" + (index+1)); } else { System.out.println("元素 40 不在数组中"); } } }
输出
元素 40 在数组中的位置 4