Java Array Equal arraysEqual(final Object[] a1, final Object[] a2)

Here you can find the source of arraysEqual(final Object[] a1, final Object[] a2)

Description

Determines whether the two Object arrays are component-wise equal.

License

Open Source License

Declaration

public static boolean arraysEqual(final Object[] a1, final Object[] a2) 

Method Source Code

//package com.java2s;
/*/*from   w w w  . j  a v a 2  s .c  om*/
 * #%L
 * VisBio application for visualization of multidimensional biological
 * image data.
 * %%
 * Copyright (C) 2002 - 2014 Board of Regents of the University of
 * Wisconsin-Madison.
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-2.0.html>.
 * #L%
 */

public class Main {
    /**
     * Determines whether the two Object arrays are component-wise equal. Also
     * works with multidimensional arrays of Objects or primitives.
     */
    public static boolean arraysEqual(final Object[] a1, final Object[] a2) {
        if (a1 == null || a2 == null)
            return a1 == null && a2 == null;
        if (a1.length != a2.length)
            return false;
        if (!a1.getClass().equals(a2.getClass()))
            return false;
        if (a1.length == 0)
            return true;

        final String cname = a1[0].getClass().getName();
        final boolean array = cname.startsWith("[");
        final char atype = cname.length() < 1 ? '\0' : cname.charAt(1);
        for (int i = 0; i < a1.length; i++) {
            if (array) {
                if (atype == '[' || atype == 'L') { // array of arrays/Objects
                    if (!arraysEqual((Object[]) a1[i], (Object[]) a2[i]))
                        return false;
                } else if (atype == 'I') { // array of ints
                    if (!arraysEqual((int[]) a1[i], (int[]) a2[i]))
                        return false;
                } else if (atype == 'C') { // array of chars
                    if (!arraysEqual((char[]) a1[i], (char[]) a2[i]))
                        return false;
                } else if (atype == 'F') { // array of floats
                    if (!arraysEqual((float[]) a1[i], (float[]) a2[i]))
                        return false;
                } else if (atype == 'D') { // array of doubles
                    if (!arraysEqual((double[]) a1[i], (double[]) a2[i]))
                        return false;
                } else if (atype == 'J') { // array of longs
                    if (!arraysEqual((long[]) a1[i], (long[]) a2[i]))
                        return false;
                } else if (atype == 'S') { // array of shorts
                    if (!arraysEqual((short[]) a1[i], (short[]) a2[i]))
                        return false;
                } else if (atype == 'B') { // array of bytes
                    if (!arraysEqual((byte[]) a1[i], (byte[]) a2[i]))
                        return false;
                } else if (atype == 'Z') { // array of booleans
                    if (!arraysEqual((boolean[]) a1[i], (boolean[]) a2[i]))
                        return false;
                }
            } else if (!objectsEqual(a1[i], a2[i]))
                return false;
        }
        return true;
    }

    /** Determines whether the two boolean arrays are component-wise equal. */
    public static boolean arraysEqual(final boolean[] a1, final boolean[] a2) {
        if (a1 == null || a2 == null)
            return a1 == null && a2 == null;
        if (a1.length != a2.length)
            return false;
        for (int i = 0; i < a1.length; i++) {
            if (a1[i] != a2[i])
                return false;
        }
        return true;
    }

    /** Determines whether the two byte arrays are component-wise equal. */
    public static boolean arraysEqual(final byte[] a1, final byte[] a2) {
        if (a1 == null || a2 == null)
            return a1 == null && a2 == null;
        if (a1.length != a2.length)
            return false;
        for (int i = 0; i < a1.length; i++) {
            if (a1[i] != a2[i])
                return false;
        }
        return true;
    }

    /** Determines whether the two char arrays are component-wise equal. */
    public static boolean arraysEqual(final char[] a1, final char[] a2) {
        if (a1 == null || a2 == null)
            return a1 == null && a2 == null;
        if (a1.length != a2.length)
            return false;
        for (int i = 0; i < a1.length; i++) {
            if (a1[i] != a2[i])
                return false;
        }
        return true;
    }

    /** Determines whether the two double arrays are component-wise equal. */
    public static boolean arraysEqual(final double[] a1, final double[] a2) {
        if (a1 == null || a2 == null)
            return a1 == null && a2 == null;
        if (a1.length != a2.length)
            return false;
        for (int i = 0; i < a1.length; i++) {
            if (a1[i] != a2[i])
                return false;
        }
        return true;
    }

    /** Determines whether the two float arrays are component-wise equal. */
    public static boolean arraysEqual(final float[] a1, final float[] a2) {
        if (a1 == null || a2 == null)
            return a1 == null && a2 == null;
        if (a1.length != a2.length)
            return false;
        for (int i = 0; i < a1.length; i++) {
            if (a1[i] != a2[i])
                return false;
        }
        return true;
    }

    /** Determines whether the two int arrays are component-wise equal. */
    public static boolean arraysEqual(final int[] a1, final int[] a2) {
        if (a1 == null || a2 == null)
            return a1 == null && a2 == null;
        if (a1.length != a2.length)
            return false;
        for (int i = 0; i < a1.length; i++) {
            if (a1[i] != a2[i])
                return false;
        }
        return true;
    }

    /** Determines whether the two long arrays are component-wise equal. */
    public static boolean arraysEqual(final long[] a1, final long[] a2) {
        if (a1 == null || a2 == null)
            return a1 == null && a2 == null;
        if (a1.length != a2.length)
            return false;
        for (int i = 0; i < a1.length; i++) {
            if (a1[i] != a2[i])
                return false;
        }
        return true;
    }

    /** Determines whether the two short arrays are component-wise equal. */
    public static boolean arraysEqual(final short[] a1, final short[] a2) {
        if (a1 == null || a2 == null)
            return a1 == null && a2 == null;
        if (a1.length != a2.length)
            return false;
        for (int i = 0; i < a1.length; i++) {
            if (a1[i] != a2[i])
                return false;
        }
        return true;
    }

    /**
     * Determines whether the two objects are equal. In particular, the case where
     * one or both objects are null is handled properly.
     */
    public static boolean objectsEqual(final Object o1, final Object o2) {
        if (o1 == null && o2 == null)
            return true;
        if (o1 == null || o2 == null)
            return false;
        return o1.equals(o2);
    }
}

Related

  1. arraysAreEqual(final byte[] array1, final byte[] array2)
  2. arraysAreEqual(Object value, Object otherValue)
  3. arraysEqual(boolean[] a, boolean[] b)
  4. arraysEqual(byte array1[], byte array2[])
  5. arraysEqual(byte[] bytes, byte[] ints)
  6. arraysEqual(Object[] array1, Object[] array2)
  7. arraysEqual(String[] arr1, String[] arr2)
  8. arraysEqual(String[] one, String[] other)
  9. arraysEquals(byte[] a, byte[] b)