Search code examples
javaarraysprimitive

Java comparing Arrays


I have two Arrays of unknown type...is there a way to check the elements are the same:

public static boolean equals(Object a , Object b) {
  if (a instanceof int[])
    return Arrays.equals((int[]) a, (int[])b);
  if (a instanceof double[]){
    ////etc
}

I want to do this without all the instanceof checks....


Solution

  • ArrayUtils.isEquals() from Apache Commons does exactly that. It also handles multi-dimensional arrays.