java.util.Arrays.equals() with limited length

…衆ロ難τιáo~ 提交于 2019-12-01 08:08:07

ByteBuffer provides something similar to what @meriton proposed but can work with primitives. Here is illustrative code:

import java.nio.ByteBuffer;

public class Main {

    public static void main(String [] args) throws Exception {

        byte [] a1 = {0, 1, 0, 1};
        byte [] a2 = {0, 0, 1, 0};

        boolean eq = ByteBuffer.wrap(a1,0,3).equals(ByteBuffer.wrap(a2,1,3));
        System.out.println("equal: " + eq);
    }
}

Properties of @meriton answer:

  • Result is collection with full power of using them.
  • Actually it is somewhat copy (but not full).
  • Requires references, primitives cannot be wrapped in this way.

This answer specials.

  • Backend array is NOT CHANGED in any way. ByteBuffer.array() returns reference to original array (could be disadvantage, could be advantage).
  • It works with primitives.

You could do:

Arrays.asList(a).subList(0,n).equals(Arrays.asList(b).subList(0,n))

You could based your method throught the source code of Arrays.equals.

public static boolean equals(byte[] a, byte[] a2, int length) {
        if (a==a2)
            return true;
        if (a==null || a2==null)
            return false;

        for (int i=0; i<length; i++)
            if (a[i] != a2[i])
                return false;

        return true;
    }

This feature is added in Java 9. Given array a and array b, you would do:

//with char array
    Arrays.compare(a, startIndex_A, endIndex_A, b, startIndex_B, endIndex_B);

//with byte, short, int, long arrays
    Arrays.compareUnsigned(a, startIndex_A, endIndex_A, b, startIndex_B, endIndex_B);

Why not trivially implement it yourself?

public static void firstNEqual(byte[] a, byte[] b, int n) {
    assert a.length >= n && b.length >= n;

    for(int i = 0; i < n; i++)
        if(a[i] != b[i])
            return false;
    return true;
}

What makes you think there should be a utility method built in? What if my condition is a[1:4] == b[0:3]? Is that more readable as Arrays.subrangesEqual(a, b, 1, 0, 3), or as an explicit for loop:

for(int i = 1, j = 0, count = 0; count < 3; count++, i++, j++)
    if(a[i] != b[j])
        return false;
return true;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!