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

更新於: 24-9-2019

966 瀏覽

開啟您的 事業

完成課程以獲得認證

開始
廣告
© . All rights reserved.