比较 Java 中的两个基元数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/630808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:08:07  来源:igfitidea点击:

Compare two arrays of primitives in Java?

javaarrayscompareprimitive-types

提问by Mark Renouf

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

我知道 Arrays.deepEquals(Object[], Object[]) 但这不适用于原始类型(由于数组和自动装箱的限制,请参阅此相关帖子)。

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

回答by Jon Skeet

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

但是,对于更简单的替代方案 - 使用Arrays.equals每个原始类型都有重载。(该实现与您的非常相似,除了它将数组长度提升到循环之外。在 .NET 上,这可能是一种反优化,但我猜 JRE 库实现者可能更了解 JVM :)

回答by unwind

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

我认为最有效的应该是使用Arrays类中的辅助方法,因为它们可能会更巧妙地实现。所以在这种情况下,使用

Arrays.equals(a, b);

回答by EpicPandaForce

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.

显然array.equals(otherArray)做了 a array == otherArray,而不是你所期望的。