目录

Ints

Ints是原始类型int的实用程序类。

Class 声明 (Class Declaration)

以下是com.google.common.primitives.Ints类的声明 -

@GwtCompatible
public final class Ints
   extends Object

字段 (Fields)

Sr.No 领域和描述
1

static int BYTES

表示原始int值所需的字节数。

2

static int MAX_POWER_OF_TWO

两个最大的幂可以表示为int。

方法 (Methods)

Sr.No 方法和描述
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)

返回一个包含与array相同值的数组,但保证指定的最小长度。

7

static int fromByteArray(byte[] bytes)

返回其big-endian表示存储在前4个字节字节中的int值; 相当于ByteBuffer.wrap(bytes).getInt()。

8

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

以big-endian顺序返回其字节表示为给定的4个字节的int值; 相当于Ints.fromByteArray(new byte [] {b1,b2,b3,b4})。

9

static int hashCode(int value)

返回值的哈希码; 等于调用((整数)值).hashCode()的结果。

10

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

返回数组中值target的第一次出现的索引。

11

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

返回数组中指定目标第一次出现的起始位置,如果不存在,则返回-1。

12

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

返回一个字符串,其中包含由separator分隔的提供的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)

返回值最接近的int。

18

static Converter《String,Integer》 stringConverter()

返回一个可序列化的转换器对象,该对象使用Integer.decode(java.lang.String)和Integer.toString()在字符串和整数之间进行转换。

19

static int[] toArray(Collection《? extends Number》 collection)

返回一个包含collection的每个值的数组,以Number.intValue()的方式转换为int值。

20

static byte[] toByteArray(int value)

返回4元素字节数组中的big-endian值表示形式; 相当于ByteBuffer.allocate(4).putInt(value).array()。

21

static Integer tryParse(String string)

将指定的字符串解析为带符号的十进制整数值。

方法继承 (Methods Inherited)

该类继承以下类中的方法 -

  • java.lang.Object

Ints类的示例

使用您选择的任何编辑器在C:/》 Guava.创建以下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 
↑回到顶部↑
WIKI教程 @2018