Java Ints min() 函数

Java Ints min() 函数

Guava的 Ints .min() 返回数组中存在的 最小值

语法

public static int min(int... array)

参数。该方法以 数组 为参数,是一个非空的int值数组。

返回值: 该方法返回数组中小于或等于数组中所有其他值的值。

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

下面的例子说明了Ints.min()方法。

例1 :

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

输出:

Minimum Value is: -5

例2: 演示IllegalArgumentException

// Java code to show implementation of
// Guava's Ints.min() 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.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 : "
                               + Ints.min(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#min-int…-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程