how to compare the Java Byte[] array?

后端 未结 15 2356
囚心锁ツ
囚心锁ツ 2020-12-13 01:17
public class ByteArr {

    public static void main(String[] args){
        Byte[] a = {(byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00};
        Byte[] b = {(byte)0x         


        
相关标签:
15条回答
  • 2020-12-13 02:05

    why a[] doesn't equals b[]? Because equals function really called on Byte[] or byte[] is Object.equals(Object obj). This functions only compares object identify , don't compare the contents of the array.

    0 讨论(0)
  • 2020-12-13 02:07

    There's a faster way to do that:

    Arrays.hashCode(arr1) == Arrays.hashCode(arr2)
    
    0 讨论(0)
  • 2020-12-13 02:08

    Java byte compare,

    public static boolean equals(byte[] a, byte[] a2) {
            if (a == a2)
                return true;
            if (a == null || a2 == null)
                return false;
    
            int length = a.length;
            if (a2.length != length)
                return false;
    
            for (int i = 0; i < length; i++)
                if (a[i] != a2[i])
                    return false;
    
            return true;
        }
    
    0 讨论(0)
提交回复
热议问题