0

Java – Compare multidimensional arrays

This post will show you how to compare if two(2) multidimensional arrays are the same in terms of element values and their locations. For example, we want to compare against the original array if something was changed in the “copy” array.

Using Arrays.deepEquals with String

Here we have two multidimensional arrays and we have to know if they are the same or not.

Output

The codes output false since the two arrays are different. strCopyValues has "1" at strCopyValues[0][0] and additional element "k"

Using Arrays.deepEquals with other Reference types

Here is an example that uses the Person objects instead of String objects.

Person Class

We overriden the hashCode and equals method.

Pet Class

To keep things simple, each person has a Pet object.

Main class

Now let’s take a look at our main class that uses the Person and Pet classes.

Output

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert