Guava Ints类

来源:这里教程网 时间:2026-02-17 21:41:55 作者:

Guava Ints类

Ints 是原始类型int的一个实用类。它提供了与int基元有关的静态实用方法,这些方法在Integer或Arrays中都没有找到。

声明 :

@GwtCompatible(emulated=true)public final class Intsextends Object

下表显示了Guava Ints类的字段摘要 :


Ints类提供的一些方法是 :


异常 :

checkedCast : 如果值大于Integer.MAX_VALUE或小于Integer.MIN_VALUEmin : 如果数组是空的,会出现IllegalArgumentException。max : 如果数组为空,出现IllegalArgumentException。fromByteArray : 如果字节的元素少于4个,则出现IllegalArgumentException。ensureCapacity : 如果minLength或padding为负数,则出现IllegalArgumentException。toArray : 如果集合或其任何元素为空,则出现NullPointerException。

下表显示了Guava的Ints类提供的其他一些方法:

下面给出了一些例子,显示了Guava的Ints类方法的实现:
例子1:

// Java code to show implementation// of Guava Ints.asList() method  import com.google.common.primitives.Ints;import java.util.*;  class GFG {    // Driver method    public static void main(String[] args)    {        int arr[] = { 5, 10, 15, 20, 25 };          // Using Ints.asList() method which wraps        // the primitive integer array as List of        // integer Type        List<Integer> myList = Ints.asList(arr);          // Displaying the elements        System.out.println(myList);    }}

输出:

[5, 10, 15, 20, 25]

例子2:

// Java code to show implementation// of Guava Ints.toArray() method  import com.google.common.primitives.Ints;import java.util.*;  class GFG {    // Driver method    public static void main(String[] args)    {        List<Integer> myList = Arrays.asList(5, 10, 15, 20, 25);          // Using Ints.toArray() method which        // converts a List of Integer to an        // array of int        int[] arr = Ints.toArray(myList);          // Displaying the elements        System.out.println(Arrays.toString(arr));    }}

输出:

[5, 10, 15, 20, 25]

例子3:

// Java code to show implementation// of Guava Ints.concat() method  import com.google.common.primitives.Ints;import java.util.*;  class GFG {    // Driver method    public static void main(String[] args)    {        int[] arr1 = { 5, 10, 15 };        int[] arr2 = { 20, 25 };          // Using Ints.concat() method which        // combines arrays from specified        // arrays into a single array        int[] arr = Ints.concat(arr1, arr2);          // Displaying the elements        System.out.println(Arrays.toString(arr));    }}

输出:

[5, 10, 15, 20, 25]

例子4:

// Java code to show implementation// of Guava Ints.contains() method  import com.google.common.primitives.Ints;  class GFG {    // Driver method    public static void main(String[] args)    {        int[] arr = { 5, 10, 15, 20 };          // Using Ints.contains() method which        // checks if element is present in array        // or not        System.out.println(Ints.contains(arr, 10));        System.out.println(Ints.contains(arr, 17));    }}

输出:

truefalse

例子5:

// Java code to show implementation// of Guava Ints.min() method  import com.google.common.primitives.Ints;  class GFG {    // Driver method    public static void main(String[] args)    {        int[] arr = { 5, 10, 15, 20 };          // Using Ints.min() method        System.out.println(Ints.min(arr));    }}

输出:

5

例子6:

// Java code to show implementation// of Guava Ints.max() method  import com.google.common.primitives.Ints;  class GFG {    // Driver method    public static void main(String[] args)    {        int[] arr = { 5, 10, 15, 20 };          // Using Ints.max() method        System.out.println(Ints.max(arr));    }}

输出:

20

相关推荐