Bytes是byte的基本类型实用工具类。
以下是com.google.common.primitives.Bytes类的声明:
@GwtCompatible
public final class Bytes
extends Object
方法
这个类继承了以下类方法: java.lang.Object
使用所选择的编辑器创建下面的java程序 C:/> Guava
GuavaTester.java
import java.util.List;
import com.google.common.primitives.Bytes;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testBytes();
}
private void testBytes(){
byte[] byteArray = {1,2,3,4,5,5,7,9,9};
//convert array of primitives to array of objects
List<Byte> objectArray = Bytes.asList(byteArray);
System.out.println(objectArray.toString());
//convert array of objects to array of primitives
byteArray = Bytes.toArray(objectArray);
System.out.print("[ ");
for(int i = 0; i< byteArray.length ; i++){
System.out.print(byteArray[i] + " ");
}
System.out.println("]");
byte data = 5;
//check if element is present in the list of primitives or not
System.out.println("5 is in list? "+ Bytes.contains(byteArray, data));
//Returns the index
System.out.println("Index of 5: " + Bytes.indexOf(byteArray,data));
//Returns the last index maximum
System.out.println("Last index of 5: " + Bytes.lastIndexOf(byteArray,data));
}
}
验证结果
使用javac编译器编译如下类
C:\Guava>javac GuavaTester.java
现在运行GuavaTester看到的结果
C:\Guava>java GuavaTester
看到结果
[1, 2, 3, 4, 5, 5, 7, 9, 9]
[ 1 2 3 4 5 5 7 9 9 ]
5 is in list? true
Index of 5: 4
Last index of 5: 5