扫码一下
查看教程更方便
bytes
是原始类型字节的实用程序类。
以下是 com.google.common.primitives.bytes
类的声明
@gwtcompatible
public final class bytes
extends object
序号 | 方法 | 说明 |
---|---|---|
1 | static list |
返回由指定数组支持的固定大小列表,类似于 arrays.aslist(object[]) 。 |
2 | static byte[] concat(byte[]... arrays) | 将每个提供的数组中的值返回到一个数组中。 |
3 | static boolean contains(byte[] array, byte target) | 如果 target 作为元素出现在数组中的任何位置,则返回 true。 |
4 | static byte[] ensurecapacity(byte[] array, int minlength, int padding) | 返回一个包含与数组相同值的数组,但保证具有指定的最小长度。 |
5 | static int hashcode(byte value) | 返回值的哈希码; 等于调用 ((byte) value).hashcode() 的结果。 |
6 | static int indexof(byte[] array, byte target) | 返回值目标在数组中第一次出现的索引。 |
7 | static int indexof(byte[] array, byte[] target) | 返回指定目标在数组中第一次出现的起始位置,如果不存在则返回 -1。 |
8 | static int lastindexof(byte[] array, byte target) | 返回值目标在数组中最后一次出现的索引。 |
9 | static byte[] toarray(collection collection) | 返回一个数组,其中包含集合的每个值,以 number.bytevalue() 的方式转换为字节值。 |
该类继承了以下类的方法 -
在 c:/> guava 中使用我们选择的任何编辑器创建以下 java 程序。
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 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