Joker
Joker

Reputation: 11150

How to compare two-dimensional (or nested) 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));

    }
}

Upvotes: 10

Views: 4428

Answers (4)

Pritam Banerjee
Pritam Banerjee

Reputation: 18968

It is not working as expected because you are initializing two different objects with new.

From Java Docs:

boolean java.util.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. Two objects e1 and e2 are considered equal if (e1==null ? e2==null : e1.equals(e2)). In other words, the two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.

Parameters: a one array to be tested for equality a2 the other array to be tested for equality Returns: true if the two arrays are equal

Upvotes: 0

ajb
ajb

Reputation: 31699

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.

Upvotes: 8

Eran
Eran

Reputation: 394156

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.

Upvotes: 30

Related Questions