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

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

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

Guava 的 Shorts 类的Shorts.min()方法用于查找数组中存在的最小值。此方法返回的值是指定数组中的最小短值。

句法:

public static short min(short... array)

参数:此方法采用一个强制参数数组,该数组是一个非空的短值数组。

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

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

下面的例子说明了 Shorts 类的 min() 方法:

示例 1:

// Java code to show implementation of
// Guava's Shorts.min() 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 };
  
        // Using Shorts.min() method to get the
        // minimum value present in the array
        System.out.println("Minimum Value is: "
                           + Shorts.min(arr));
    }
}
输出:
Minimum Value is: -5

示例 2:

// Java code to show implementation of
// Guava's Shorts.min() 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 = {};
  
        try {
  
            // Using Shorts.min() method to get the
            // minimum value present in the array
            // This should raise "IllegalArgumentException"
            // as the array is empty
            System.out.println("Minimum Value is: "
                               + Shorts.min(arr));
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
java.lang.IllegalArgumentException

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