Guava - Ints 類



Ints 是一個用於基本型別 int 的工具類。

類宣告

以下是com.google.common.primitives.Ints類的宣告:

@GwtCompatible
public final class Ints
   extends Object

欄位

序號 欄位及描述
1

static int BYTES

表示一個基本 int 值所需的位元組數。

2

static int MAX_POWER_OF_TWO

可以表示為 int 的最大的 2 的冪。

方法

序號 方法及描述
1

static List<Integer> asList(int... backingArray)

返回一個由指定陣列支援的固定大小列表,類似於 Arrays.asList(Object[])。

2

static int checkedCast(long value)

如果可能,返回等於 value 的 int 值。

3

static int compare(int a, int b)

比較兩個指定的 int 值。

4

static int[] concat(int[]... arrays)

返回將每個提供的陣列中的值組合到單個數組中的值。

5

static boolean contains(int[] array, int target)

如果 target 在陣列中的任何位置作為元素存在,則返回 true。

6

static int[] ensureCapacity(int[] array, int minLength, int padding)

返回一個包含與陣列相同值的陣列,但保證具有指定的最小長度。

7

static int fromByteArray(byte[] bytes)

返回其大端表示形式儲存在 bytes 的前 4 個位元組中的 int 值;等效於 ByteBuffer.wrap(bytes).getInt()。

8

static int fromBytes(byte b1, byte b2, byte b3, byte b4)

返回其位元組表示形式為給定 4 個位元組的 int 值,以大端順序排列;等效於 Ints.fromByteArray(new byte[] {b1, b2, b3, b4})。

9

static int hashCode(int value)

返回 value 的雜湊碼;等於呼叫 ((Integer) value).hashCode() 的結果。

10

static int indexOf(int[] array, int target)

返回 target 在陣列中第一次出現的索引。

11

static int indexOf(int[] array, int[] target)

返回陣列中指定 target 的第一次出現的起始位置,如果不存在則返回 -1。

12

static String join(String separator, int... array)

返回一個包含由分隔符分隔的提供的 int 值的字串。

13

static int lastIndexOf(int[] array, int target)

返回 target 在陣列中最後一次出現的索引。

14

static Comparator<int[]> lexicographicalComparator()

返回一個比較器,用於按字典順序比較兩個 int 陣列。

15

static int max(int... array)

返回陣列中存在的最大值。

16

static int min(int... array)

返回陣列中存在的最小值。

17

static int saturatedCast(long value)

返回最接近 value 的 int 值。

18

static Converter<String,Integer> stringConverter()

返回一個可序列化的轉換器物件,該物件使用 Integer.decode(java.lang.String) 和 Integer.toString() 在字串和整數之間進行轉換。

19

static int[] toArray(Collection<? extends Number> collection)

返回一個包含集合中每個值的陣列,以 Number.intValue() 的方式轉換為 int 值。

20

static byte[] toByteArray(int value)

返回 value 在 4 元素位元組陣列中的大端表示形式;等效於 ByteBuffer.allocate(4).putInt(value).array()。

21

static Integer tryParse(String string)

將指定的字串解析為帶符號的十進位制整數值。

繼承的方法

此類繼承自以下類:

  • java.lang.Object

Ints 類的示例

使用您選擇的任何編輯器建立以下 Java 程式,例如在C:/> Guava.

GuavaTester.java

import java.util.List;

import com.google.common.primitives.Ints;

public class GuavaTester {

   public static void main(String args[]) {
      GuavaTester tester = new GuavaTester();
      tester.testInts();
   }

   private void testInts() {
      int[] intArray = {1,2,3,4,5,6,7,8,9};

      //convert array of primitives to array of objects
      List<Integer> objectArray = Ints.asList(intArray);
      System.out.println(objectArray.toString());

      //convert array of objects to array of primitives
      intArray = Ints.toArray(objectArray);
      System.out.print("[ ");
      
      for(int i = 0; i< intArray.length ; i++) {
         System.out.print(intArray[i] + " ");
      }
      
      System.out.println("]");
      
      //check if element is present in the list of primitives or not
      System.out.println("5 is in list? " + Ints.contains(intArray, 5));

      //Returns the minimum		
      System.out.println("Min: " + Ints.min(intArray));

      //Returns the maximum		
      System.out.println("Max: " + Ints.max(intArray));

      //get the byte array from an integer
      byte[] byteArray = Ints.toByteArray(20000);
      
      for(int i = 0; i< byteArray.length ; i++) {
         System.out.print(byteArray[i] + " ");
      }
   }
}

驗證結果

使用javac編譯器編譯類,如下所示:

C:\Guava>javac GuavaTester.java

現在執行 GuavaTester 以檢視結果。

C:\Guava>java GuavaTester

檢視結果。

[1, 2, 3, 4, 5, 6, 7, 8, 9]
[ 1 2 3 4 5 6 7 8 9 ]
5 is in list? true
Min: 1
Max: 9
0 0 78 32 
guava_primitive_utilities.htm
廣告

© . All rights reserved.