简体   繁体   English

如何比较 Java 中的两个 object arrays?

[英]How to compare two object arrays in Java?

I have two object arrays like so:我有两个 object arrays 像这样:

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

I would like to know if the arrays are equal.我想知道 arrays 是否相等。 I'm defining equal as every value in array1 is the same as the value in that position in the array2.我定义为相等,因为 array1 中的每个值都与 array2 中的 position 中的值相同。 So these two arrays would be equal.所以这两个 arrays 将相等。

What is the best why to find out if these two arrays are equal?找出这两个 arrays 是否相等的最佳方法是什么?

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.编辑:我需要一个可以将 go 转换为嵌套 arrays 的等号。

Use Arrays.deepEquals() .使用Arrays.deepEquals() This does the same job as Arrays.equals() but also works with nested arrays.这与Arrays.equals()的作用相同,但也适用于嵌套的 arrays。

Returns true if the two specified arrays are deeply equal to one another.如果两个指定的 arrays 彼此深度相等,则返回 true。 Unlike the equals(Object[],Object[]) method, this method is appropriate for use with nested arrays of arbitrary depth.equals(Object[],Object[])方法不同,此方法适用于任意深度的嵌套 arrays。

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.如果两个数组引用都是 null,或者如果它们引用包含相同数量元素的 arrays 并且两个 arrays 中的所有对应元素对深度相等,则认为两个数组引用深度相等。

Two possibly null elements e1 and e2 are deeply equal if any of the following conditions hold:如果满足以下任何条件,则两个可能的 null 元素 e1 和 e2 非常相等:

  • e1 and e2 are both arrays of object reference types, and Arrays.deepEquals(e1, e2) would return true e1 和 e2 都是 object 引用类型的 arrays 和 Arrays.deepEquals(e1, e2) 将返回 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 是相同原始类型的 arrays,并且 Arrays.equals(e1, e2) 的适当重载将返回 true。
  • e1 == e2 e1 == e2
  • e1.equals(e2) would return true. e1.equals(e2) 将返回 true。

Note that this definition permits null elements at any depth.请注意,此定义允许 null 元素位于任何深度。

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.如果指定的 arrays 中的任何一个通过 arrays 的一个或多个级别直接或间接包含自己作为元素,则此方法的行为未定义。

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:为了比较 arrays,我将使用 Arrays.equals 方法:

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.在您发布的示例中, arrays 实际上将包含Integer对象。 In that case, Arrays.equals() is enough.在这种情况下, Arrays.equals()就足够了。 However, if your arrays contain some objects of yours, you have to implement equals() in your class so that Arrays.equals() work但是,如果您的 arrays 包含您的某些对象,则必须在 class 中实现equals()以便Arrays.equals()工作

array1.equals(array2) should give you what you are looking for. array1.equals(array2)应该给你你正在寻找的东西。

Generally utility class java.util.Arrays is very usefull.一般实用程序 class 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 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 . 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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM