Domanda

Conosco Arrays.deepEquals (Object [], Object []) ma questo non funziona per i tipi primitivi (a causa delle limitazioni di array e autoboxing, vedi questo post correlato ).

Con questo in mente, è questo l'approccio più efficiente?

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;
}
È stato utile?

Soluzione

Modifica il tuo primo confronto in:

if (a == b)
    return true;

Questo non solo cattura " sia null " casi, ma anche "confrontare un array con se stesso" caso.

Tuttavia, per un'alternativa più semplice, utilizzare Arrays.equals che presenta sovraccarichi per ogni tipo di primitiva. (L'implementazione è molto simile alla tua, tranne per il fatto che solleva la lunghezza dell'array dal ciclo. Su .NET può essere un'anti-ottimizzazione, ma immagino che gli implementatori della libreria JRE probabilmente conoscano meglio la JVM :)

Altri suggerimenti

Penso che il più efficace dovrebbe essere quello di utilizzare i metodi di supporto in Array , perché potrebbero essere implementati in modo più intelligente. Quindi, in questo caso, usa

Arrays.equals(a, b);

Non so se questo possa aiutare qualcuno, ma sembra che funzioni:

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

Apparentemente array.equals (otherArray) fa un array == otherArray , e non quello che ti aspetteresti.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top