Guava – 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…-