如何比较 Java 中的两个对象数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6652085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 12:23:25  来源:igfitidea点击:

How to compare two object arrays in Java?

javaarraysobjectequals

提问by Grammin

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 equal. I'm defining equal as every value in array1 is the same as the value in that position in the array2. So these two arrays would be equal.

我想知道数组是否相等。我定义等于 array1 中的每个值都与 array2 中那个位置的值相同。所以这两个数组是相等的。

What is the best why to find out if these two arrays are equal?

找出这两个数组是否相等的最佳原因是什么?

if(array1 == array2) 

is not a deep equals so that won't work and I don't know if looping over each element and comparing them is the best and most efficient way to solve this problem. Does anyone have any better suggestions?

不是一个深等于所以不起作用,我不知道循环每个元素并比较它们是否是解决这个问题的最好和最有效的方法。有人有更好的建议吗?

Edit: I needed an equals that can go into nested arrays.

编辑:我需要一个可以进入嵌套数组的 equals。

采纳答案by Marcelo

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

使用Arrays.deepEquals(). 这Arrays.equals()与嵌套数组的作用相同,但也适用于嵌套数组。

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.

如果两个指定的数组彼此深度相等,则返回 true。与该equals(Object[],Object[])方法不同,该方法适用于任意深度的嵌套数组。

如果两个数组引用都为空,或者它们引用的数组包含相同数量的元素并且两个数组中的所有对应元素对都深度相等,则认为两个数组引用深度相等。

如果满足以下任一条件,则两个可能为空的元素 e1 和 e2 非常相等:

  • e1 和 e2 都是对象引用类型的数组,Arrays.deepEquals(e1, e2) 将返回 true
  • e1 和 e2 是相同基本类型的数组,适当重载 Arrays.equals(e1, e2) 将返回 true。
  • e1 == e2
  • e1.equals(e2) 将返回 true。

请注意,此定义允许任何深度的空元素。

如果任一指​​定数组直接或间接通过一个或多个数组级别包含它们自己作为元素,则此方法的行为是未定义的。

回答by adarshr

java.util.Arrays.equals

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) 

回答by Kyle

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

为了比较数组,我会使用 Arrays.equals 方法:

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

回答by user756212

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

array1.equals(array2)应该给你你正在寻找的东西。

回答by zacheusz

Generally utility class java.util.Arraysis very usefull.

通常实用程序类java.util.Arrays非常有用。

  • 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.
  • 如果认为两个数组相等,则两个数组包含相同数量的元素,并且两个数组中的所有元素对都相等,请使用Arrays.equals
  • 如果两个数组引用都为空,则被认为是深度相等的,或者如果它们引用包含相同数量元素的数组,并且两个数组中所有对应的元素对都非常相等,则使用Arrays.deepEquals。此方法适用于任意深度的嵌套数组。

回答by Op De Cirkel

In the example you've posted the arrays will actually contain Integerobjects. 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

在您发布的示例中,数组实际上将包含Integer对象。那样的话,Arrays.equals()就够了。但是,如果您的数组包含您的某些对象,则必须equals()在您的类中实现,以便Arrays.equals()工作