📜  整数 max()函数|番石榴 |Java

📅  最后修改于: 2022-05-13 01:54:24.594000             🧑  作者: Mango

整数 max()函数|番石榴 |Java

Guava 的Ints.max()返回数组中存在的最大值

句法:

public static int max(int... 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…-