Description
Determines if the given two arrays are equal, as determined by comparing their contents for equality.
License
Apache License
Parameter
Parameter | Description |
---|
array1 | an array |
array2 | another array |
Exception
Parameter | Description |
---|
IllegalArgumentException | if array1 is not actually an array |
Return
true if array2 is an array of the same type as array1 and has equal elements
Declaration
public static boolean equals(Object array1, Object array2)
Method Source Code
//package com.java2s;
//License from project: Apache License
import java.util.Arrays;
public class Main {
/**// ww w.j a v a2 s. c om
* Determines if the given two arrays are equal, as determined by comparing their contents for
* equality. This provides a convenience above and beyond similar methods in {@link Arrays}
* because it accepts any array type, including primitive array types.
*
* @param array1 an array
* @param array2 another array
* @return true if {@code array2} is an array of the same type as {@code array1} and has equal
* elements
* @throws IllegalArgumentException if {@code array1} is not actually an array
*
* @see Arrays#equals(Object[], Object[])
*/
public static boolean equals(Object array1, Object array2) {
Class<?> arrayType = array1.getClass();
if (!arrayType.isArray()) {
throw new IllegalArgumentException("specified object is not an array");
}
Class<?> componentType = arrayType.getComponentType();
if (componentType.isPrimitive()) {
if (componentType == boolean.class) {
return array2 instanceof boolean[] && Arrays.equals((boolean[]) array1, (boolean[]) array2);
} else if (componentType == byte.class) {
return array2 instanceof byte[] && Arrays.equals((byte[]) array1, (byte[]) array2);
} else if (componentType == char.class) {
return array2 instanceof char[] && Arrays.equals((char[]) array1, (char[]) array2);
} else if (componentType == short.class) {
return array2 instanceof short[] && Arrays.equals((short[]) array1, (short[]) array2);
} else if (componentType == int.class) {
return array2 instanceof int[] && Arrays.equals((int[]) array1, (int[]) array2);
} else if (componentType == long.class) {
return array2 instanceof long[] && Arrays.equals((long[]) array1, (long[]) array2);
} else if (componentType == float.class) {
return array2 instanceof float[] && Arrays.equals((float[]) array1, (float[]) array2);
} else if (componentType == double.class) {
return array2 instanceof double[] && Arrays.equals((double[]) array1, (double[]) array2);
} else {
throw new AssertionError("Unrecognized primitive type: " + componentType);
}
} else {
return array2 instanceof Object[] && Arrays.equals((Object[]) array1, (Object[]) array2);
}
}
}
Related
- equals(long[] a, long[] a2)
- equals(Map a, Map b)
- equals(Map map1, Map map2)
- equals(Object array1, Object array2)
- equals(Object array1, Object array2)
- equals(Object[] objs1, Object[] objs2)
- equals(String[] fullSet, String[] subSet)
- equals(T[] array1, T[] array2)
- equals3(double[][][] arr1, double[][][] arr2)