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.
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.
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.
array1.equals(array2)
should give you what you are looking for.