Ints 的 lastIndexOf() 函式在 Java 中


Ints 類的 lastIndexOf() 方法返回陣列中 target 值最後出現時的索引。語法如下 −

public static int
lastIndexOf(int[] arr, int target)

其中,arr 是整數值陣列,target 是我們正在查詢其最後索引的整數值。

我們先來看一個例子 −

示例

import com.google.common.primitives.Ints;
class Demo {
   public static void main(String[] args) {
      int[] myArr = { 10, 20, 30, 40, 50, 60,20, 80, 20, 100 };
      System.out.println(Ints.join("-", myArr));
      // finding last index of element 20
      int index = Ints.lastIndexOf(myArr, 20);
      if (index != -1) {
         System.out.println("The last index of element 20 = " + index);
      } else {
         System.out.println("The element 20 is not in the array.");
      }
   }
}

輸出

10-20-30-40-50-60-20-80-20-100
The last index of element 20 = 8

更新日期:2019 年 9 月 24 日

90 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.