gpt4 book ai didi

java - 字节数组是二维字节数组吗?

转载 作者:行者123 更新时间:2023-12-02 04:18:52 24 4
gpt4 key购买 nike

我有一个字节数组:

private final static byte[][] ARRAY = {
{ (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd },
{ (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44 }
};

给定一个任意字节数组:

byte[] check = { (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44) };

byte[] check2 = { (byte) 0x12, (byte) 0x23, (byte) 0x34, (byte) 0x45) };

检查 checkcheck2 是否在 ARRAY 中与所写的完全一致(以相同的顺序等)的最佳方法是什么?

我可以根据需要将 ARRAY 更改为任何其他数据结构,但 checkcheck2 作为字节数组提供。

最佳答案

您可以使用Arrays.equals()来比较两个数组。来自 javadoc :

Returns true if the two specified arrays of bytes are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal.

byte[] check = { (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44 };
byte[] check2 = { (byte) 0x12, (byte) 0x23, (byte) 0x34, (byte) 0x45 };

int i = 0;
for (; i < ARRAY.length; i++) {
if (Arrays.equals(ARRAY[i], check)) {
System.out.println("check[] found at index: " + i);
break;
}
}
if (i == ARRAY.length) {
System.out.println("check[] not found");
}

for (i = 0; i < ARRAY.length; i++) {
if (Arrays.equals(ARRAY[i], check2)) {
System.out.println("check2[] found at index: " + i);
break;
}
}
if (i == ARRAY.length) {
System.out.println("check2[] not found");
}

输出:

check[] found at index: 1
check2[] not found

关于java - 字节数组是二维字节数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33006535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com