Java 字节数组是否在2d字节数组中?

Java 字节数组是否在2d字节数组中?,java,Java,我有一个字节数组: 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) }; 给定任意

我有一个字节数组:

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) };
给定任意字节数组:

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) };

检查
check
check2
ARRAY
中是否与所写内容完全一致(顺序相同等)的最佳方法是什么


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

您可以使用
数组.equals
方法

Arrays.equals(arr1,arr2)
将返回
true
如果两个数组具有相同顺序的相同字节

在Java 8中,一种相当好的方法可以查看
字节[][]
是否包含特定的
字节[]
arr

Stream.of(ARRAY).filter(j -> Arrays.equals(arr, j)).findAny().isPresent();

在Java8之前,您必须为循环使用

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

如果指定的两个字节数组等于一,则返回true 另一个如果两个数组都包含 相同数量的元素,以及中所有对应的元素对 这两个数组相等


使用byte[]包装类和HashSet

在包装类中,使用数组.equals(byte[],byte[])和数组.hashCode(byte[])重写
equals()
hashCode()
,然后HashSet将匹配具有完全相同元素的其他字节数组

像这样:

public static void main(String[] args) {
    Set<MyArray> ARRAY_SET = new HashSet<MyArray>();
    ARRAY_SET.add(new MyArray(new byte[] { (byte) 0xaa, (byte) 0xbb,
            (byte) 0xcc, (byte) 0xdd }));
    ARRAY_SET.add(new MyArray(new byte[] { (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 };
    System.out.println(ARRAY_SET.contains(new MyArray(check)));//true
    System.out.println(ARRAY_SET.contains(new MyArray(check2)));//false
}

static class MyArray {
    private byte[] array;

    public MyArray(byte[] array) {
        super();
        this.array = array;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + Arrays.hashCode(array);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        MyArray other = (MyArray) obj;
        if (!Arrays.equals(array, other.array))
            return false;
        return true;
    }
}
publicstaticvoidmain(字符串[]args){
Set ARRAY_Set=new HashSet();
数组_SET.add(新MyArray(新字节[]{(字节)0xaa,(字节)0xbb,
(字节)0xcc,(字节)0xdd});
数组_SET.add(新MyArray(新字节[]{(字节)0x11,(字节)0x22,
(字节)0x33,(字节)0x44});
字节[]检查={(字节)0x11,(字节)0x22,(字节)0x33,(字节)0x44};
字节[]检查2={(字节)0x12,(字节)0x23,(字节)0x34,(字节)0x45};
System.out.println(ARRAY_SET.contains(new MyArray(check));//true
System.out.println(ARRAY_SET.contains(new MyArray(check2));//false
}
静态类MyArray{
专用字节[]数组;
公共MyArray(字节[]数组){
超级();
this.array=数组;
}
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
结果=素数*结果+数组。哈希代码(数组);
返回结果;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
如果(getClass()!=obj.getClass())
返回false;
MyArray other=(MyArray)obj;
如果(!array.equals(array,other.array))
返回false;
返回true;
}
}

所以我需要循环,没有
数组。包含
类型函数或任何东西?您必须循环,因为您要检查数组的子维度是否相等。若要检查另一个2D数组是否等于数组,可以使用deepEquals()。
public static void main(String[] args) {
    Set<MyArray> ARRAY_SET = new HashSet<MyArray>();
    ARRAY_SET.add(new MyArray(new byte[] { (byte) 0xaa, (byte) 0xbb,
            (byte) 0xcc, (byte) 0xdd }));
    ARRAY_SET.add(new MyArray(new byte[] { (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 };
    System.out.println(ARRAY_SET.contains(new MyArray(check)));//true
    System.out.println(ARRAY_SET.contains(new MyArray(check2)));//false
}

static class MyArray {
    private byte[] array;

    public MyArray(byte[] array) {
        super();
        this.array = array;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + Arrays.hashCode(array);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        MyArray other = (MyArray) obj;
        if (!Arrays.equals(array, other.array))
            return false;
        return true;
    }
}