Java 中的 Ints max() 函式


Ints 類的 max() 方法返回陣列中存在的最值。以下是語法 −

static int max(int... array)

讓我們先看一個例子 −

示例

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));
      System.out.println("Maximum value from the array = " + Ints.max(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.");
      }
   }
}

輸出

Maximum value from the array = 100
10-20-30-40-50-60-20-80-20-100
The last index of element 20 = 8

更新於: 2019-09-24

130 次瀏覽

開啟您的 職業生涯

透過完成學習課程獲得認證

開始
廣告