How to compare two object arrays in Java?

做~自己de王妃 提交于 2019-11-28 09:12:38

Use Arrays.deepEquals(). This does the same job as Arrays.equals() but also works with nested arrays.

Returns true if the two specified arrays are deeply equal to one another. Unlike the equals(Object[],Object[]) method, this method is appropriate for use with nested arrays of arbitrary depth.

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.

Two possibly null elements e1 and e2 are deeply equal if any of the following conditions hold:

  • e1 and e2 are both arrays of object reference types, and Arrays.deepEquals(e1, e2) would return true
  • e1 and e2 are arrays of the same primitive type, and the appropriate overloading of Arrays.equals(e1, e2) would return true.
  • e1 == e2
  • e1.equals(e2) would return true.

Note that this definition permits null elements at any depth.

If either of the specified arrays contain themselves as elements either directly or indirectly through one or more levels of arrays, the behavior of this method is undefined.

java.util.Arrays.equals

   /**
     * Returns <tt>true</tt> if the two specified arrays of Objects are
     * <i>equal</i> to one another.  The two arrays are considered equal if
     * both arrays contain the same number of elements, and all corresponding
     * pairs of elements in the two arrays are equal.  Two objects <tt>e1</tt>
     * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
     * : e1.equals(e2))</tt>.  In other words, the two arrays are equal if
     * they contain the same elements in the same order.  Also, two array
     * references are considered equal if both are <tt>null</tt>.<p>
     *
     * @param a one array to be tested for equality.
     * @param a2 the other array to be tested for equality.
     * @return <tt>true</tt> if the two arrays are equal.
     */
    public static boolean equals(Object[] a, Object[] a2) 

To compare arrays, I would use the Arrays.equals method:

if (Arrays.equals(array1, array2))
{    
  // array1 and array2 contain the same elements in the same order
}

In the example you've posted the arrays will actually contain Integer objects. In that case, Arrays.equals() is enough. However, if your arrays contain some objects of yours, you have to implement equals() in your class so that Arrays.equals() work

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.
user756212

array1.equals(array2) should give you what you are looking for.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!