Java Array Equal equals(Object array1, Object array2)

Here you can find the source of equals(Object array1, Object array2)

Description

See if the two arrays are "equal" (not identidy, but that the contents are equal).

License

Open Source License

Parameter

Parameter Description
array1 must be an array
array2 must be an array

Return

true if semantically equal using ;

Declaration

public static boolean equals(Object array1, Object array2) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2001, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * //  w  w w. j a  va2s . co m
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.util.Arrays;

public class Main {
    /**
     * See if the two arrays are "equal" (not identidy, but that the contents are equal).
     * @param array1 must be an array
     * @param array2 must be an array
     * @return true if semantically equal using {@link Arrays#equals(Object[], Object[])};
     * 
     * @see Arrays#equals(Object[], Object[]);
     * @since 1.2.1
     */
    public static boolean equals(Object array1, Object array2) {
        if (array1 == array2)
            return true;
        if (array1 == null || array2 == null)
            return false;
        Class aclass = array1.getClass();
        Class bclass = array2.getClass();
        if (!aclass.isArray() || !bclass.isArray())
            return false;
        Class acomp = aclass.getComponentType();
        Class bcomp = bclass.getComponentType();
        if (acomp.isPrimitive() || bcomp.isPrimitive()) {
            if (acomp != bcomp)
                return false;
            if (acomp == Integer.TYPE)
                return Arrays.equals((int[]) array1, (int[]) array2);
            else if (acomp == Boolean.TYPE)
                return Arrays
                        .equals((boolean[]) array1, (boolean[]) array2);
            else if (acomp == Long.TYPE)
                return Arrays.equals((long[]) array1, (long[]) array2);
            else if (acomp == Short.TYPE)
                return Arrays.equals((short[]) array1, (short[]) array2);
            else if (acomp == Double.TYPE)
                return Arrays.equals((double[]) array1, (double[]) array2);
            else if (acomp == Float.TYPE)
                return Arrays.equals((float[]) array1, (float[]) array2);
            else if (acomp == Character.TYPE)
                return Arrays.equals((char[]) array1, (char[]) array2);
            else if (acomp == Byte.TYPE)
                return Arrays.equals((byte[]) array1, (byte[]) array2);
            else
                return false;
        } else
            return Arrays.equals((Object[]) array1, (Object[]) array2);
    }
}

Related

  1. equals(int[][] ar1, int[][] ar2)
  2. equals(long[] a, long[] a2)
  3. equals(Map a, Map b)
  4. equals(Map map1, Map map2)
  5. equals(Object array1, Object array2)
  6. equals(Object array1, Object array2)
  7. equals(Object[] objs1, Object[] objs2)
  8. equals(String[] fullSet, String[] subSet)
  9. equals(T[] array1, T[] array2)