Guava教程
整数Ints是原始的int类型的实用工具类。
以下是com.google.common.primitives.Ints类的声明:
@GwtCompatible
public final class Ints
extends Object
字段
方法
继承的方法
这个类继承了以下类方法:java.lang.Object
使用所选择的任何编辑器创建下面的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
转载自并发编程网-ifeve.com