Java Array Equal arrayEquals(Object[] a, Object[] b)

Here you can find the source of arrayEquals(Object[] a, Object[] b)

Description

Compare two arrays of objects for equality.

License

Open Source License

Parameter

Parameter Description
a an array of objects
b another array of objects

Return

true if a and b are the same size and have contents that compare as the same element-wise, or are both null, false otherwise

Declaration

public static boolean arrayEquals(Object[] a, Object[] b) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from   ww  w  .j a va2  s .c o m*/
     * Compare two arrays of objects for equality.
     * 
     * @param a   an array of objects
     * @param b   another array of objects
     * @return true if a and b are the same size and have contents that compare
     *         as the same element-wise, or are both null, false otherwise
     */
    public static boolean arrayEquals(Object[] a, Object[] b) {
        if (a == null) {
            return b == null;
        }
        if (b == null) {
            return false;
        }
        if (a.length != b.length) {
            return false;
        }
        for (int i = 0; i < a.length; i++) {
            if (!equals(a[i], b[i])) {
                return false;
            }
        }
        return true;
    }

    /**
     * Compare two potentially null references for equality.
     *  
     * @param a  a reference
     * @param b  another reference
     * @return true if a and b are either equal or both null, false otherwise
     */
    public static <E> boolean equals(E a, E b) {
        if (a == null) {
            return b == null;
        }
        if (b == null) {
            return false;
        }
        return a.equals(b);
    }
}

Related

  1. arrayEquals(byte[] data, byte[] expected)
  2. arrayEquals(byte[] left, byte[] right)
  3. arrayEquals(int[] a1, int[] a2)
  4. arrayEquals(int[] arr1, int[] arr2)
  5. arrayEquals(Object[] a, Object[] b)
  6. arrayEquals(Object[] arr1, Object[] arr2)
  7. arrayEquals(Object[] array1, Object[] array2)
  8. arrayEquals(Object[] source, Object target)
  9. arrayEquals(Object[] source, Object target)