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