Question: I have a byte array:

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

```

Given an arbitrary byte array:

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

```

or

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

```

What's the best way to check if either `check` or `check2` is in `ARRAY` exactly as written (in the same order etc)?

I can change `ARRAY` to any other data structure as needed but `check` and `check2` are provided as byte arrays.

Answer: 
You can use `Arrays.equals()` to compare two arrays. From the [javadoc](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#equals(byte[],%20byte[])):

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

```

***Output :***

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

```
You can use the `Arrays.equals` method.

`Arrays.equals(arr1, arr2)` will return `true` if the two arrays have the same bytes in the same order.

In Java 8 a reasonably nice way to see if a `byte[][]` contains a particular `byte[]` `arr` is

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

```

Before Java 8, you would have to use a `for` loop.