Java如何使用Guava Ints.min()函数?用法示例详解

2021年3月27日15:05:55 发表评论 728 次浏览

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

语法如下:

public static int min(int... array)

参数:此方法需要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…-


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: