Guava Ints 类

返回 Guava Primitive 实用程序


Ints 是基本类型 int 的实用程序类。


字段

序号 字段 说明
1 static int BYTES 表示原始 int 值所需的字节数。
2 static int MAX_POWER_OF_TWO 可以表示为 int 的最大的两个幂。

类声明

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

@GwtCompatible
public final class Ints
   extends Object

类方法

序号 方法 说明
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) 如果目标作为元素出现在数组中的任何位置,则返回 true。
6 static int[] ensureCapacity(int[] array, int minLength, int padding) 返回包含与数组相同值的数组,但保证具有指定的最小长度。
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) 返回 int 值,其字节表示为给定的 4 个字节,按大端顺序排列; 相当于 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) 返回值最接近值的整数。
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) 返回 4 元素字节数组中值的大端表示; 相当于 ByteBuffer.allocate(4).putInt(value).array()
21 static Integer tryParse(String string) 将指定的字符串解析为带符号的十进制整数值。

方法继承

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

  • java.lang.Object

Ints 类示例

C:/> Guava 中使用我们选择的任何编辑器创建以下 java 程序。

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};

      //将基元数组转换为对象数组
      List<Integer> objectArray = Ints.asList(intArray);
      System.out.println(objectArray.toString());

      //将对象数组转换为基元数组
      intArray = Ints.toArray(objectArray);
      System.out.print("[ ");
      
      for(int i = 0; i< intArray.length ; i++) {
         System.out.print(intArray[i] + " ");
      }
      
      System.out.println("]");
      
      //检查元素是否存在于基元列表中
      System.out.println("5 is in list? " + Ints.contains(intArray, 5));

      //返回最小值
      System.out.println("Min: " + Ints.min(intArray));

      //返回最大值
      System.out.println("Max: " + Ints.max(intArray));

      //从整数中获取字节数组
      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 实用程序

查看笔记

扫码一下
查看教程更方便