equals vs Arrays.equals in Java

前端 未结 8 1423
走了就别回头了
走了就别回头了 2020-11-21 23:29

When comparing arrays in Java, are there any differences between the following 2 statements?

Object[] array1, array2;
array1.equals(array2);
Arrays.equals(ar         


        
8条回答
  •  星月不相逢
    2020-11-21 23:56

    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) compares the contents of the arrays.


    Similarly array.toString() may not be very useful and you need to use Arrays.toString(array).

提交回复
热议问题