views:

1101

answers:

4

I know about Arrays.deepEquals(Object[], Object[]) but this doesn't work for primitive types (due limitations of arrays and autoboxing, see this related post).

With that in mind, is this the most efficient approach?

boolean byteArrayEquals(byte[] a, byte[] b) {
    if (a == null && b == null)
        return true;

    if (a == null || b == null)
        return false;

    if (a.length != b.length)
        return false;

    for (int i = 0; i < a.length; i++) {
        if (a[i] != b[i])
            return false;
    }
    return true;
}
+13  A: 

Change your first comparison to be:

if (a == b)
    return true;

This not only catches the "both null" cases, but also "compare an array to itself" case.

However, for a simpler alternative - use Arrays.equals which has overloads for each primitive type. (The implementation is very similar to yours, except it hoists the array length out of the loop. On .NET that can be an anti-optimization, but I guess the JRE library implementors probably know better for the JVM :)

Jon Skeet
I've been Skeeted!
Michael Myers
I thought there was a library method, but tweakt threw me off by mentioning deepEquals() right off the bat. Arrays.equals() does exactly what this method with your change does, although it also stores a.length in a temporary variable (before comparing lengths).
Michael Myers
Thanks! And I knew some of this, thanks for reminding me... man SO has made me lazy!
Mark Renouf
You may find the VM ignore the implementation in the library and inline its own.
Tom Hawtin - tackline
+3  A: 

I think the most efficient should be to use the helper methods in the Arrays class, because they might be implemented more cleverly. So in this case, use

Arrays.equal(a, b);

and be done.

unwind
A: 

I'd wonder what raw arrays are doing outside of an object. A good object-oriented design would encapsulate and hide your choice of array over a data structure like a Collection or List. The fact that you're dealing with raw arrays makes me think that your design isn't sufficiently object-oriented.

I don't mean this as a criticism, just a suggestion. When I find myself dealing too much with primitives, I try to think how I might encapsulate things better. And to me, a String is a primitive. Given a choice between a raw String and a PostalCode, for example, I think I'm better off if I can encapsulate the rules defining a proper value inside a class.

duffymo
A: 

How do you know that those raw arrays aren't inside of an object?

No matter how "sufficiently" object-oriented your program is, there are more than a few occasions where one needs to implement those objects types in the first place, and the best data type is frequently a primitive.

Ben Goldberg