4 November 2011

How to compare if 2 arrays has equal members?

This is note to myself (because I always forgot about this very easy way to compare Arrays)

How to check is 2 arrays has equal members in the same order?
Arrays.equals(a1, a2);

for example:
boolean isEqual = Arrays.equals(a1, a2);

for example 2:
if(Arrays.equals(a1, a2)){
  System.out.print("They are equal.");
  System.out.print("They are NOT equal.Really.they are different.I know is hard to live with that,but true is they are different.");

From Oracle JavaDoc:
"The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal.(...) In other words, the two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null"