Guava – Shorts.max()方法及实例
Shorts.max() 是Guava库中Shorts类的一个方法,用于寻找数组中的最大值。该方法返回的值是指定数组中最大的短值。
语法:
public static short max(short... array)
参数:该方法需要一个强制参数数组,是一个非空的短值数组。
返回值: 该方法返回一个短值,是指定数组中的最大值。
异常情况:如果数组为空,该方法会抛出IllegalArgumentException。
下面的程序说明了上述方法的使用。
例子-1 :
// Java code to show implementation of// Guava's Shorts.max() methodimport 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() methodimport 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…-
