📌  相关文章
📜  Java番石榴 |带有示例的 Floats.min() 方法

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

Java番石榴 |带有示例的 Floats.min() 方法

Floats.min()是 Guava 库中 Floats 类的一种方法,用于查找数组中存在的最小值。此方法返回的值是指定数组中的最小浮点值。

句法:

public static float min(float... array)

参数:此方法采用强制参数数组,该数组是浮点值的非空数组。

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

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

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

示例 1:

// Java code to show implementation of
// Guava's Floats.min() method
  
import com.google.common.primitives.Floats;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
        // Creating a float array
        float[] arr = { 2.2f, 4.3f, 6.4f, 10.5f,
                        0.6f, -5.7f, 15.8f };
  
        // Using Floats.min() method to get the
        // minimum value present in the array
        System.out.println("Minimum Value is : "
                           + Floats.min(arr));
    }
}
输出:
Minimum Value is : -5.7

示例 2:

// Java code to show implementation of
// Guava's Floats.min() method
  
import com.google.common.primitives.Floats;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a float array
        float[] arr = {};
  
        try {
            // Using Floats.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 : "
                               + Floats.min(arr));
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
java.lang.IllegalArgumentException

参考: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/primitives/Floats.html#min(float…)