Compare two arrays of primitives in Java?

删除回忆录丶 提交于 2019-11-26 14:28:05

问题


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;
}

回答1:


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 :)




回答2:


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.equals(a, b);



回答3:


I don't know if this will help anyone, but this seems to be working:

        if(type == type_BooleanArray) {
            boolean eq = Arrays.equals((boolean[]) thisObj, (boolean[]) thatObj);
            if(!eq) {
                return false;
            }
        } else if(type == type_ByteArray) {
            boolean eq = Arrays.equals((byte[]) thisObj, (byte[]) thatObj);
            if(!eq) {
                return false;
            }
        } else if(type == type_ShortArray) {
            boolean eq = Arrays.equals((short[]) thisObj, (short[]) thatObj);
            if(!eq) {
                return false;
            }
        } else if(type == type_CharArray) {
            boolean eq = Arrays.equals((char[]) thisObj, (char[]) thatObj);
            if(!eq) {
                return false;
            }
        } else if(type == type_IntArray) {
            boolean eq = Arrays.equals((int[]) thisObj, (int[]) thatObj);
            if(!eq) {
                return false;
            }
        } else if(type == type_LongArray) {
            boolean eq = Arrays.equals((long[]) thisObj, (long[]) thatObj);
            if(!eq) {
                return false;
            }
        } else if(type == type_FloatArray) {
            boolean eq = Arrays.equals((float[]) thisObj, (float[]) thatObj);
            if(!eq) {
                return false;
            }
        } else if(type == type_DoubleArray) {
            boolean eq = Arrays.equals((double[]) thisObj, (double[]) thatObj);
            if(!eq) {
                return false;
            }
        } else {
            if(!thisObj.equals(thatObj)) {
                return false;
            }
        }

Apparently array.equals(otherArray) does a array == otherArray, and not what you would expect.



来源:https://stackoverflow.com/questions/630808/compare-two-arrays-of-primitives-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!