Java中的equals vs Arrays.equals
在Java中比较数组时,下面2个语句有区别吗?
When comparing arrays in Java, are there any differences between the following 2 statements?
Object[] array1, array2;
array1.equals(array2);
Arrays.equals(array1, array2);
如果是这样,它们是什么?
And if so, what are they?
推荐答案
array1.equals(array2)
和 array1 == array2
一样,即是相同的数组.正如@alf 指出的那样,这不是大多数人所期望的.
array1.equals(array2)
is the same as array1 == array2
, i.e. is it the same array. As @alf points out it's not what most people expect.
Arrays.equals(array1, array2)
比较数组的内容.
同样array.toString()
可能不是很有用,需要使用Arrays.toString(array)
.
Similarly array.toString()
may not be very useful and you need to use Arrays.toString(array)
.
相关文章