12

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.

1
  • 1
    I don't think there is anyway around a "for-each"-loop. To make the if-statement working you might want to define your own class aryClass with an isEqual()-function. But I know you are looking for something better, so I leave this as a comment rather than an answer.
    – AudioDroid
    Jul 11, 2011 at 15:03

6 Answers 6

21

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.

5
  • @Op De Cirkel I did read it, and now added to my answer. Is there something wrong with the method?
    – Marcelo
    Jul 11, 2011 at 15:05
  • Not really, just deepEquals() does too much if you do not need it (slow, as it has to check if the elements are arrays.) Anyway, I discarded the downvote Jul 11, 2011 at 15:08
  • @Op de Cirkel You are right about the overhead, but the OP doesn't specify what kind of elements are in the arrays. I guess this is just part of the answer for the question.
    – Marcelo
    Jul 11, 2011 at 15:11
  • +1 Yeah, depending on what you want, either Arrays.equals() or Arrays.deepEquals does it. Jul 11, 2011 at 15:13
  • I was able to do an unique class/method/line/object id based on stacktrace and this, thx! Nov 12, 2014 at 23:03
9

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) 
0
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
}
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

0

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

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.