Java: How to test on array equality?

前端 未结 4 1422
清酒与你
清酒与你 2020-11-27 06:04

Why is the following code printing \"Different.\"?

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


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 06:10

    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.

提交回复
热议问题