当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Guava Ints max()用法及代码示例


Guava 的Ints.max()返回数组中的最大值。

用法:

public static int max(int... array)

参数:此方法将数组作为参数,它是int值的非空数组。


返回值:此方法返回数组中存在的大于或等于数组中其他每个值的值。

异常:如果array为空,则该方法引发IllegalArgumentException。

以下示例说明了Ints.max()方法:

范例1:

// Java code to show implementation of 
// Guava's Ints.max() 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, 7 }; 
  
        // Using Ints.max() method to get the 
        // maximum value present in the array 
        System.out.println("Maximum value is:"
                           + Ints.max(arr)); 
    } 
}
输出:
Maximum value is:15

范例2:演示IllegalArgumentException

// Java code to show implementation of 
// Guava's Ints.max() 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.max() method to get the 
            // maximum value present in the array 
            // This should raise "IllegalArgumentException" 
            // as the array is empty 
            System.out.println("Maximum value is:"
                               + Ints.max(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#max-int…-



相关用法


注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Ints max() function | Guava | Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。