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.
编辑:我需要一个可以进入嵌套数组的等号。
6 个解决方案
#1
19
Use Arrays.deepEquals()
. This does the same job as Arrays.equals()
but also works with nested arrays.
使用Arrays.deepEquals()。这与array .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.如果指定的两个数组彼此非常相等,则返回true。与equals(Object[],Object[])方法不同,此方法适用于任意深度的嵌套数组。
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,或者它们引用的数组包含相同数量的元素,并且两个数组中所有对应的元素对都是深度相等的,那么两个数组引用就被认为是深度相等的。
Two possibly null elements e1 and e2 are deeply equal if any of the following conditions hold:
两个可能为零的元素e1和e2在下列条件中任何一个成立时都是完全相等的:
- e1 and e2 are both arrays of object reference types, and Arrays.deepEquals(e1, e2) would return true
- e1和e2都是对象引用类型的数组,而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是具有相同基元类型的数组,并且具有适当的数组重载。等号(e1, e2)将返回true。
- e1 == e2
- e1 = = e2
- e1.equals(e2) would return true.
- . equals(e2)将返回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.
如果指定的数组中的任何一个通过一个或多个数组直接或间接地将自己包含为元素,则此方法的行为没有定义。
#2
9
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)
#3
3
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
}
#4
2
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
在您发布的示例中,数组实际上包含整数对象。在这种情况下,Arrays.equals()就足够了。但是,如果数组包含一些对象,则必须在类中实现equals(),以便使用array .equals()
#5
0
Generally utility class java.util.Arrays is very usefull.
一般java.util实用程序类。数组是非常有用的。
- 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.
- 如果两个数组引用都为null,或者它们引用的数组包含相同数量的元素和两个数组中所有相应的元素对,那么使用arrays . deepequals就完全相同了。此方法适用于任意深度的嵌套数组。
#6
0
array1.equals(array2)
should give you what you are looking for.
array1.equals(array2)应该能够提供您所需要的信息。
#1
19
Use Arrays.deepEquals()
. This does the same job as Arrays.equals()
but also works with nested arrays.
使用Arrays.deepEquals()。这与array .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.如果指定的两个数组彼此非常相等,则返回true。与equals(Object[],Object[])方法不同,此方法适用于任意深度的嵌套数组。
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,或者它们引用的数组包含相同数量的元素,并且两个数组中所有对应的元素对都是深度相等的,那么两个数组引用就被认为是深度相等的。
Two possibly null elements e1 and e2 are deeply equal if any of the following conditions hold:
两个可能为零的元素e1和e2在下列条件中任何一个成立时都是完全相等的:
- e1 and e2 are both arrays of object reference types, and Arrays.deepEquals(e1, e2) would return true
- e1和e2都是对象引用类型的数组,而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是具有相同基元类型的数组,并且具有适当的数组重载。等号(e1, e2)将返回true。
- e1 == e2
- e1 = = e2
- e1.equals(e2) would return true.
- . equals(e2)将返回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.
如果指定的数组中的任何一个通过一个或多个数组直接或间接地将自己包含为元素,则此方法的行为没有定义。
#2
9
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)
#3
3
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
}
#4
2
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
在您发布的示例中,数组实际上包含整数对象。在这种情况下,Arrays.equals()就足够了。但是,如果数组包含一些对象,则必须在类中实现equals(),以便使用array .equals()
#5
0
Generally utility class java.util.Arrays is very usefull.
一般java.util实用程序类。数组是非常有用的。
- 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.
- 如果两个数组引用都为null,或者它们引用的数组包含相同数量的元素和两个数组中所有相应的元素对,那么使用arrays . deepequals就完全相同了。此方法适用于任意深度的嵌套数组。
#6
0
array1.equals(array2)
should give you what you are looking for.
array1.equals(array2)应该能够提供您所需要的信息。