Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two-dimensional (or nested) Java Arrays?

Tags:

java

arrays

From the Java docs for Arrays.equals(Object[] a, Object[] a2):

Returns true if the two specified arrays of Objects are equal to one another. The 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.

But when I ran the program below it is printing false as output.

So, Does the mean equals method of the Array class not work for multidimensional arrays?

What API can I use to achieve true as the result in the program below?

public class Test {
    public static void main(String[] args) {
        String[][] rows1 = { new String[] { "a", "a" } };

        String[][] rows2 = { new String[] { "a", "a" } };

        System.out.println("Arrays.equals() = " + Arrays.equals(rows1, rows2));

    }
}
like image 816
Sachin Sachdeva Avatar asked Jun 01 '17 05:06

Sachin Sachdeva


People also ask

How do you compare two-dimensional arrays in Java?

In short, to compare two dimensional arrays we have implemented a method as described below: The example's method is boolean equal(final int[][] arr1, final int[][] arr2) . The method takes as parameters two int arrays, and returns a boolean , that is true if the arrays are equal and false otherwise.

How do you compare multidimensional arrays?

To compare array's structure, You should use identity operator. The only difference(s) between this and my answer is that instead of using == for the elements in the array, it will use === , and with === it checks the order of key value pairs.

How do I compare two double arrays in Java?

Arrays. equals(double[] a, double[] a2) method returns true if the two specified arrays of doubles are equal to one another. Two arrays are equal if they contain the same elements in the same order.


2 Answers

You are comparing two dimensional arrays, which means the elements of these arrays are themselves arrays. Therefore, when the elements are compared (using Object's equals), false is returned, since Object's equals compares Object references.

Use Arrays.deepEquals instead.

From the Javadoc:

boolean java.util.Arrays.deepEquals(Object[] a1, Object[] a2)

Returns true if the two specified arrays are deeply equal to one another. Unlike the equals(Object [], Object []) method, this method is appropriate for use with nested arrays of arbitrary depth.

like image 130
Eran Avatar answered Nov 15 '22 15:11

Eran


Arrays.deepEquals().

Here's why Arrays.equals doesn't work. As the doc says, the arrays have to have the same number of elements, and the elements have to be equals. The arrays do have the same number of elements: 1. Each element is another array.

However, those arrays are compared with the regular equals method. And for any object, if the object doesn't override the equals method defined for Object, then the equals method defined for Object is used, which is the same as ==. And arrays don't override equals (they also don't override toString(), which is why we have to use Arrays.toString() to format an array).

Arrays.deepEquals() makes a special check for when elements are arrays, and then it uses a recursive Arrays.deepEquals() to test those arrays for equality.

like image 25
ajb Avatar answered Nov 15 '22 14:11

ajb