Java Array Equal equals(Object array1, Object array2)

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

Description

equals

License

Open Source License

Declaration

static boolean equals(Object array1, Object array2) 

Method Source Code


//package com.java2s;

import java.util.Arrays;

public class Main {
    static boolean equals(Object array1, Object array2) {
        boolean equal = false;
        //If the same array or both null
        if (array1 == array2) {
            equal = true;/*from  ww w .j a  v a  2  s . co  m*/
        }
        //If both non-null and of the same class
        else if (array1 != null && array2 != null && array1.getClass().equals(array2.getClass())) {
            if (array1 instanceof byte[] && array2 instanceof byte[]) {
                equal = Arrays.equals((byte[]) array1, (byte[]) array2);
            } else if (array1 instanceof short[] && array2 instanceof short[]) {
                equal = Arrays.equals((short[]) array1, (short[]) array2);
            } else if (array1 instanceof int[] && array2 instanceof int[]) {
                equal = Arrays.equals((int[]) array1, (int[]) array2);
            } else if (array1 instanceof long[] && array2 instanceof long[]) {
                equal = Arrays.equals((byte[]) array1, (byte[]) array2);
            } else if (array1 instanceof float[] && array2 instanceof float[]) {
                equal = Arrays.equals((float[]) array1, (float[]) array2);
            } else if (array1 instanceof double[] && array2 instanceof double[]) {
                equal = Arrays.equals((double[]) array1, (double[]) array2);
            } else if (array1 instanceof boolean[] && array2 instanceof boolean[]) {
                equal = Arrays.equals((boolean[]) array1, (boolean[]) array2);
            } else if (array1 instanceof char[] && array2 instanceof char[]) {
                equal = Arrays.equals((char[]) array1, (char[]) array2);
            } else if (array1 instanceof Object[] && array2 instanceof Object[]) {
                equal = Arrays.equals((Object[]) array1, (Object[]) array2);
            } else {
                throw new IllegalArgumentException("One or more of the values provided are not an array\n"
                        + "array1 class: " + array1.getClass() + "\n" + "array2 class: " + array2.getClass());
            }
        }

        return equal;
    }
}

Related

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