Java:如何测试数组相等性?

发布于 2024-12-14 02:12:10 字数 317 浏览 1 评论 0原文

为什么下面的代码打印“Different.”

boolean[][] a = { {false,true}, {true,false} };
boolean[][] b = { {false,true}, {true,false} };

if (Arrays.equals(a, b) || a == b)
    System.out.println("Equal.");
else
    System.out.println("Different.");

Why is the following code printing "Different."?

boolean[][] a = { {false,true}, {true,false} };
boolean[][] b = { {false,true}, {true,false} };

if (Arrays.equals(a, b) || a == b)
    System.out.println("Equal.");
else
    System.out.println("Different.");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

为什么下面的代码打印“Different.”

因为Arrays.equals 执行浅 比较。由于数组从 Object 继承其 equals 方法,因此将对内部数组执行身份比较,这将失败,因为 a 和 < code>b 不引用相同数组。

如果更改为 Arrays.deepEquals 它将按预期打印 "Equal."

Why is the following code printing "Different."?

Because Arrays.equals performs a shallow comparison. Since arrays inherit their equals-method from Object, an identity comparison will be performed for the inner arrays, which will fail, since a and b do not refer to the same arrays.

If you change to Arrays.deepEquals it will print "Equal." as expected.

土豪 2024-12-21 02:12:10

这确实不明显。

首先,== 运算符只是比较两个指针。由于 ab 是位于不同内存地址的不同对象,因此 a == b 将返回 false (嘿,Java 纯粹主义者,我知道 == 实际上是比较对象身份,我只是想说教一下。

现在让我们看一下数组的 equals() 实现:

boolean[] c = new boolean[] { false, true, false };
boolean[] d = new boolean[] { false, true, false };

if (c.equals(d)) {
    System.out.println("Equals");
} else {
    System.out.println("Not equals");
}

这会打印 Not equals 因为没有数组实例实际实现equals() 方法。因此,当我们调用 .equals() 时,我们实际上是在调用 Object.equals() 方法,该方法只是比较两个指针。

也就是说,请注意您的代码实际上是这样做的:

boolean[] a0 = new boolean[] { false, true };
boolean[] a1 = new boolean[] { true, false };
boolean[] b0 = new boolean[] { false, true };
boolean[] b1 = new boolean[] { true, false };
boolean[][] a = new boolean[][] { a0, a1 };
boolean[][] b = new boolean[][] { b0, b1 };

if (Arrays.equals(a, b) || a == b)
    System.out.println("Equal.");
else
    System.out.println("Different.");

Arrays.equals(a, b) 最终将调用 a0.equals(b0) ,它将返回 。因此,Arrays.equals(a, b) 也将返回 false

因此,您的代码将打印 Different.,我们得出结论,Java 相等性有时可能很棘手。

It's really not obvious.

First of all, the == operator just compare two pointers. Because a and b are distinct objects located at different memory addresses, a == b will return false (Hey, Java purists, I know that the == actually compare object identities. I'm just trying to be didactic).

Now let's take a look at the equals() implementation of arrays:

boolean[] c = new boolean[] { false, true, false };
boolean[] d = new boolean[] { false, true, false };

if (c.equals(d)) {
    System.out.println("Equals");
} else {
    System.out.println("Not equals");
}

That would print Not equals because no array instance actually implements the equals() method. So, when we call <somearray>.equals(<otherarray>) we are actually calling the Object.equals() method, which just compare two pointers.

That said, notice that your code is actually doing this:

boolean[] a0 = new boolean[] { false, true };
boolean[] a1 = new boolean[] { true, false };
boolean[] b0 = new boolean[] { false, true };
boolean[] b1 = new boolean[] { true, false };
boolean[][] a = new boolean[][] { a0, a1 };
boolean[][] b = new boolean[][] { b0, b1 };

if (Arrays.equals(a, b) || a == b)
    System.out.println("Equal.");
else
    System.out.println("Different.");

The Arrays.equals(a, b) will eventually call a0.equals(b0) which will return false. For this reason, Arrays.equals(a, b) will return false as well.

So your code will print Different. and we conclude that Java equality can be tricky sometimes.

天暗了我发光 2024-12-21 02:12:10

对多维数组使用 Arrays.deepEquals()。

Use Arrays.deepEquals() for multidimensional arrays.

谜兔 2024-12-21 02:12:10
public static boolean equal(double[][] a, double[][] b) {
        if (a == null) {
            return (b == null);
        }
        if (b == null) {
            return false;  // already know 'a' isn't null
        }
        if (a.length != b.length) {
            return false;
        }
        for (int i = 0; i < a.length; i++) {
            if (!Arrays.equals(a[i], b[i])) {
                return false;
            }
        }
        return true;
    }
public static boolean equal(double[][] a, double[][] b) {
        if (a == null) {
            return (b == null);
        }
        if (b == null) {
            return false;  // already know 'a' isn't null
        }
        if (a.length != b.length) {
            return false;
        }
        for (int i = 0; i < a.length; i++) {
            if (!Arrays.equals(a[i], b[i])) {
                return false;
            }
        }
        return true;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文