如何比较二维(或嵌套)Java数组?

时间:2021-07-10 04:48:16

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

来自Java文档的数组。equals(对象[][]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.

如果两个指定的对象数组相等,则返回true。如果两个数组包含相同数量的元素,并且两个数组中所有对应的元素对都是相等的,那么这两个数组就被认为是相等的。

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

但是当我运行下面的程序时,它会打印false。

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

数组类的equals方法不适用于多维数组吗?

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

在下面的程序中,我可以使用什么API来实现true ?

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

    }
}

4 个解决方案

#1


28  

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.

您正在比较两个维度数组,这意味着这些数组的元素本身就是数组。因此,当比较元素(使用对象的等号)时,返回false,因为对象的等号比较对象引用。

Use Arrays.deepEquals instead.

使用Arrays.deepEquals代替。

From the Javadoc:

从Javadoc:

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

布尔java.util.Arrays.deepEquals(对象[]a1,对象[]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.

如果指定的两个数组彼此非常相等,则返回true。与equals(Object [], Object[])方法不同,此方法适用于任意深度的嵌套数组。

#2


8  

Arrays.deepEquals().

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.

这就是为什么数组。=不工作。正如doc所说,数组必须有相同数量的元素,元素必须是相等的。数组的元素数量是相同的:1。每个元素都是另一个数组。

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).

但是,这些数组与常规的equals方法进行了比较。对于任何对象,如果对象不覆盖为对象定义的equals方法,则使用定义为对象的equals方法,该方法与==相同。并且数组不会重写equals(它们也不会覆盖toString(),这就是为什么我们必须使用Arrays.toString()来格式化一个数组)。

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.

deepequals()对元素是数组时进行特殊检查,然后使用递归的array . deepequals()来测试这些数组是否相等。

#3


0  

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

它没有按预期工作,因为您正在用new初始化两个不同的对象。

From Java Docs:

从Java文档:

boolean java.util.Arrays.equals(Object[] a, Object[] a2)

布尔java.util.Arrays。equals(对象[][]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.

如果两个指定的对象数组相等,则返回true。如果两个数组包含相同数量的元素,并且两个数组中所有对应的元素对都是相等的,那么这两个数组就被认为是相等的。如果(e1= null ?e2 = = null:. equals(e2))。换句话说,如果两个数组以相同的顺序包含相同的元素,那么它们是相等的。此外,如果两个数组引用都为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

参数:要测试相等a2的一个数组,以测试相等的结果:如果两个数组是相等的,则为真。

#4


0  

Please refer Arrays.equals vs Arrays.deepEquals

请参考阵列。= vs Arrays.deepEquals

http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#deepEquals(java.lang.Object[],%20java.lang.Object[])

http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html deepEquals(java . lang . object[],% 20 java . lang . object[])

#1


28  

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.

您正在比较两个维度数组,这意味着这些数组的元素本身就是数组。因此,当比较元素(使用对象的等号)时,返回false,因为对象的等号比较对象引用。

Use Arrays.deepEquals instead.

使用Arrays.deepEquals代替。

From the Javadoc:

从Javadoc:

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

布尔java.util.Arrays.deepEquals(对象[]a1,对象[]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.

如果指定的两个数组彼此非常相等,则返回true。与equals(Object [], Object[])方法不同,此方法适用于任意深度的嵌套数组。

#2


8  

Arrays.deepEquals().

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.

这就是为什么数组。=不工作。正如doc所说,数组必须有相同数量的元素,元素必须是相等的。数组的元素数量是相同的:1。每个元素都是另一个数组。

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).

但是,这些数组与常规的equals方法进行了比较。对于任何对象,如果对象不覆盖为对象定义的equals方法,则使用定义为对象的equals方法,该方法与==相同。并且数组不会重写equals(它们也不会覆盖toString(),这就是为什么我们必须使用Arrays.toString()来格式化一个数组)。

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.

deepequals()对元素是数组时进行特殊检查,然后使用递归的array . deepequals()来测试这些数组是否相等。

#3


0  

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

它没有按预期工作,因为您正在用new初始化两个不同的对象。

From Java Docs:

从Java文档:

boolean java.util.Arrays.equals(Object[] a, Object[] a2)

布尔java.util.Arrays。equals(对象[][]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.

如果两个指定的对象数组相等,则返回true。如果两个数组包含相同数量的元素,并且两个数组中所有对应的元素对都是相等的,那么这两个数组就被认为是相等的。如果(e1= null ?e2 = = null:. equals(e2))。换句话说,如果两个数组以相同的顺序包含相同的元素,那么它们是相等的。此外,如果两个数组引用都为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

参数:要测试相等a2的一个数组,以测试相等的结果:如果两个数组是相等的,则为真。

#4


0  

Please refer Arrays.equals vs Arrays.deepEquals

请参考阵列。= vs Arrays.deepEquals

http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#deepEquals(java.lang.Object[],%20java.lang.Object[])

http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html deepEquals(java . lang . object[],% 20 java . lang . object[])