Guava Ints – max() 函数

来源:这里教程网 时间:2026-02-17 21:41:46 作者:

Guava Ints max() 函数

Guava的 Ints .max() 返回数组中存在的 greatest 值。

语法

public static int max(int... array)

参数: 该方法以 array 为参数,是一个非空的int值数组。

返回值: 该方法返回数组中大于或等于数组中所有其他值的值。

异常情况: 如果数组为空,该方法会抛出IllegalArgumentException。

下面的例子说明了Ints.max()方法。

示例1:

// Java code to show implementation of// Guava's Ints.max() method  import com.google.common.primitives.Ints;import java.util.Arrays;  class GFG {      // Driver's code    public static void main(String[] args)    {        // Creating an integer array        int[] arr = { 2, 4, 6, 10, 0, -5, 15, 7 };          // Using Ints.max() method to get the        // maximum value present in the array        System.out.println("Maximum value is: "                           + Ints.max(arr));    }}

输出:

Maximum value is: 15

示例2: 为了证明IllegalArgumentException

// Java code to show implementation of// Guava's Ints.max() method  import com.google.common.primitives.Ints;import java.util.Arrays;  class GFG {      // Driver's code    public static void main(String[] args)    {          try {            // Creating an integer array            int[] arr = {};              // Using Ints.max() method to get the            // maximum value present in the array            // This should raise "IllegalArgumentException"            // as the array is empty            System.out.println("Maximum value is: "                               + Ints.max(arr));        }        catch (Exception e) {            System.out.println("Exception: " + e);        }    }}

输出:

Exception: java.lang.IllegalArgumentException

参考: https://google.github.io/guava/releases/22.0/api/docs/com/google/common/primitives/Ints.html#max-int…-

相关推荐