整数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
上一篇:
Guava Shorts类
下一篇:
Guava Longs类
