Java 中的 ints max() 函数

java 8object oriented programmingprogramming更新于 2025/6/26 13:07:17

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("数组中的最大值 = " + Ints.max(myArr));
      // 查找元素 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.");
      }
   }
}

输出

数组中的最大值 = 100
10-20-30-40-50-60-20-80-20-100
The last index of element 20 = 8

相关文章