📌  相关文章
📜  Java番石榴 | Shorts.max() 方法和示例

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

Java番石榴 | Shorts.max() 方法和示例

Shorts.max()是 Guava 库中 Shorts 类的一个方法,用于查找数组中存在的最大值。此方法返回的值是指定数组中最大的短值。

句法:

public static short max(short... array)

参数:这个方法需要一个强制参数数组是一个非空的值数组。

返回值:此方法返回一个短值,它是指定数组中的最大值。

异常:如果数组为空,该方法将抛出IllegalArgumentException

下面的程序说明了上述方法的使用:

示例 1:

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

示例 2:

// Java code to show implementation of
// Guava's Shorts.max() method
import com.google.common.primitives.Shorts;
import java.util.Arrays;
  
class GFG {
    // Driver's code
    public static void main(String[] args)
    {
        // Creating a short array
        short[] arr = {};
  
        // Using Shorts.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 : " + Shorts.max(arr));
    }
}

输出:

Exception in thread "main" java.lang.IllegalArgumentException
    at com.google.common.base.Preconditions.checkArgument(Preconditions.java:108)
    at com.google.common.primitives.Shorts.max(Shorts.java:254)
    at GFG.main(File.java:19)

参考https://google.github.io/guava/releases/23.0/api/docs/com/google/common/primitives/Shorts.html#max-short…-