📜  整数类 |番石榴 |Java

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

整数类 |番石榴 |Java

Ints是原始类型 int 的实用程序类。它提供了与 int 原语有关的静态实用方法,这些方法在整数或数组中都没有。

宣言 :

@GwtCompatible(emulated=true)
public final class Ints
extends Object

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

Ints Class 提供的一些方法是:

例外:

  • checkedCast : IllegalArgumentException如果值大于 Integer.MAX_VALUE 或小于 Integer.MIN_VALUE
  • min : IllegalArgumentException如果数组为空。
  • max : IllegalArgumentException如果数组为空。
  • fromByteArray : IllegalArgumentException如果 bytes 的元素少于 4 个。
  • ensureCapacity : IllegalArgumentException如果 minLength 或 padding 为负数。
  • 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 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 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));
    }
}

输出 :

true
false

示例 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