为什么下面的代码打印出"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."
?
因为Arrays.equals
执行浅比较。由于数组从 Object
继承了它们的 equals
方法,因此将对内部数组执行身份比较,这将失败,因为 a
和 b
不引用 same 数组。
如果您更改为 Arrays.deepEquals
它将按预期打印 "Equal."
。
关于Java:如何测试数组相等性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8051084/