How to compare two object arrays in Java?

后端 未结 6 934
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 19:46

I have two object arrays like so:

Object[] array1 = {0, 1, 2, 3};
Object[] array2 = {0, 1, 2, 3};

I would like to know if the arrays are eq

6条回答
  •  忘掉有多难
    2020-12-09 19:57

    Generally utility class java.util.Arrays is very usefull.

    • If the two arrays are considered equal both arrays contain the same number of elements, and all pairs of elements in the two arrays are equal use Arrays.equals.
    • If two array references are considered deeply equal if both are null, or if they refer to arrays that contain the same number of elements and all corresponding pairs of elements in the two arrays are deeply equal use Arrays.deepEquals. This method is appropriate for use with nested arrays of arbitrary depth.

提交回复
热议问题