Java comparing Arrays

DD. picture DD. · Oct 23, 2011 · Viewed 18.4k times · Source

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....

Answer

Tomasz Nurkiewicz picture Tomasz Nurkiewicz · Oct 23, 2011

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