Guava Ints類


整數Ints是原始的int型別的實用工具類。

類宣告

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

@GwtCompatible
public final class Ints
   extends Object

欄位

S.N. 欄位及說明
1 static int BYTES
所需要的位元組數來表示一個原始int值。
2 static int MAX_POWER_OF_TWO
兩個最大的冪可以被表示為整數。

方法

S.N. 方法及說明
1 static List<Integer> asList(int... backingArray)
返回由指定陣列支援的固定大小的列表,類似Arrays.asList(Object[]).
2 static int checkedCast(long 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)
返回true,如果target是否存在在任何地方陣列元素。
6 static int[] ensureCapacity(int[] array, int minLength, int padding)
返回一個包含相同的值陣列的陣列,但保證是一個規定的最小長度。
7 static int fromByteArray(byte[] bytes)
返回int值,其大端表示儲存在第一個4位元組的位元組;相當於ByteBuffer.wrap(bytes).getInt().
8 static int fromBytes(byte b1, byte b2, byte b3, byte b4)
返回int值的位元組表示的是給定的4個位元組,在big-endian的順序;相當於 Ints.fromByteArray(new byte[] {b1, b2, b3, b4}).
9 static int hashCode(int value)
返回值的雜湊碼; 等於呼叫 ((Integer) value).hashCode() 的結果
10 static int indexOf(int[] array, int target)
返回值目標陣列的第一次亮相的索引。
11 static int indexOf(int[] array, int[] target)
返回指定目標的第一個匹配的起始位置陣列內,或-1,如果不存在。
12 static String join(String separator, int... array)
返回包含由分離器分離所提供的整型值的字串。
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)
返回最接近的int值。
18 static Converter<String,Integer> stringConverter()
返回使用字串和整數之間的一個轉換器序列化物件 Integer.decode(java.lang.String) 和 Integer.toString().
19 static int[] toArray(Collection<? extends Number> collection)
返回包含集合的每個值的陣列,轉換為int值的方式Number.intValue().
20 static byte[] toByteArray(int 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