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

发布于 2024-07-14 18:10:06 字数 604 浏览 4 评论 0原文

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

考虑到这一点,这是最有效的方法吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等你爱我 2024-07-21 18:10:06

将您的第一个比较更改为:

if (a == b)
    return true;

这不仅捕获“均为空”的情况,而且还捕获“将数组与其自身进行比较”的情况。

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

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

笑叹一世浮沉 2024-07-21 18:10:06

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

Arrays.equals(a, b);

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);
愿与i 2024-07-21 18:10:06

我不知道这是否会对任何人有帮助,但这似乎有效:

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文