鍍金池/ 教程/ Java/ Guava Ints類
Guava原語(yǔ)工具
Guava集合工具
Guava Chars類
Guava Shorts類
Guava CharMatcher類
Guava BigIntegerMath類
Guava Range類
Guava Bimap接口
Guava緩存工具
Guava Longs類
Guava Multiset接口
Guava Table接口
Guava Optional類
Guava LongMath類
Guava Spiltter類
Guava Preconditions類
Guava數(shù)學(xué)工具
Guava Ints類
Guava Ordering類
Guava Throwables類
Guava字符串工具
Guava Objects類
Guava Booleans類
Guava教程
Guava Bytes類
Guava CaseFormat類
Guava環(huán)境設(shè)置
Guava Doubles類
Guava Joiner類
Guava Multimap類
Guava Floats類
Guava IntMath類

Guava Ints類

整數(shù)Ints是原始的int類型的實(shí)用工具類。

類聲明

以下是com.google.common.primitives.Ints類的聲明:

@GwtCompatible
public final class Ints
   extends Object

字段

S.N. 字段及說(shuō)明
1 static int BYTES
所需要的字節(jié)數(shù)來(lái)表示一個(gè)原始int值。
2 static int MAX_POWER_OF_TWO
兩個(gè)最大的冪可以被表示為整數(shù)。

方法

S.N. 方法及說(shuō)明
1 static List<Integer> asList(int... backingArray)
返回由指定數(shù)組支持的固定大小的列表,類似Arrays.asList(Object[]).
2 static int checkedCast(long value)
返回int值等于值,如果可能的話。
3 static int compare(int a, int b)
比較兩個(gè)指定的int值。
4 static int[] concat(int[]... arrays)
每個(gè)陣列提供組合成一個(gè)單一的陣列,則返回值。
5 static boolean contains(int[] array, int target)
返回true,如果target是否存在在任何地方數(shù)組元素。
6 static int[] ensureCapacity(int[] array, int minLength, int padding)
返回一個(gè)包含相同的值數(shù)組的數(shù)組,但保證是一個(gè)規(guī)定的最小長(zhǎng)度。
7 static int fromByteArray(byte[] bytes)
返回int值,其大端表示存儲(chǔ)在第一個(gè)4字節(jié)的字節(jié);相當(dāng)于ByteBuffer.wrap(bytes).getInt().
8 static int fromBytes(byte b1, byte b2, byte b3, byte b4)
返回int值的字節(jié)表示的是給定的4個(gè)字節(jié),在big-endian的順序;相當(dāng)于 Ints.fromByteArray(new byte[] {b1, b2, b3, b4}).
9 static int hashCode(int value)
返回值的哈希碼; 等于調(diào)用 ((Integer) value).hashCode() 的結(jié)果
10 static int indexOf(int[] array, int target)
返回值目標(biāo)數(shù)組的第一次亮相的索引。
11 static int indexOf(int[] array, int[] target)
返回指定目標(biāo)的第一個(gè)匹配的起始位置數(shù)組內(nèi),或-1,如果不存在。
12 static String join(String separator, int... array)
返回包含由分離器分離所提供的整型值的字符串。
13 static int lastIndexOf(int[] array, int target)
返回target 在數(shù)組中最后一個(gè)出場(chǎng)的索引值。
14 static Comparator<int[]> lexicographicalComparator()
返回一個(gè)比較,比較兩個(gè)int數(shù)組字典順序。
15 static int max(int... array)
返回出現(xiàn)在數(shù)組中的最大值。
16 static int min(int... array)
返回最小值出現(xiàn)在數(shù)組。
17 static int saturatedCast(long value)
返回最接近的int值。
18 static Converter<String,Integer> stringConverter()
返回使用字符串和整數(shù)之間的一個(gè)轉(zhuǎn)換器序列化對(duì)象 Integer.decode(java.lang.String) 和 Integer.toString().
19 static int[] toArray(Collection<? extends Number> collection)
返回包含集合的每個(gè)值的數(shù)組,轉(zhuǎn)換為int值的方式Number.intValue().
20 static byte[] toByteArray(int value)
返回一個(gè)4元素的字節(jié)數(shù)組值大端表示;相當(dāng)于 ByteBuffer.allocate(4).putInt(value).array().
21 static Integer tryParse(String string)
解析指定的字符串作為符號(hào)十進(jìn)制整數(shù)。

繼承的方法

這個(gè)類繼承了以下類方法:

  • java.lang.Object

Ints 示例

使用所選擇的任何編輯器創(chuàng)建下面的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] + " ");
      }
   }
}

驗(yàn)證結(jié)果

使用javac編譯器編譯如下類

C:\Guava>javac GuavaTester.java

現(xiàn)在運(yùn)行GuavaTester看到的結(jié)果

C:\Guava>java GuavaTester

看到結(jié)果。

[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 LongMath類下一篇:Guava Table接口