Java Array Comparison
Posted
by BlairHippo
on Stack Overflow
See other posts from Stack Overflow
or by BlairHippo
Published on 2010-03-22T15:59:45Z
Indexed on
2010/03/22
16:01 UTC
Read the original article
Hit count: 385
Working within Java, let's say I have two objects that, thanks to obj.getClass().isArray()
, I know are both arrays. Let's further say that I want to compare those two arrays to each other -- possibly by using Arrays.equals
. Is there a graceful way to do this without resorting to a big exhaustive if/else tree to figure out which flavor of Arrays.equals
needs to be used? I'm looking for something that's less of an eyesore than this:
if (obj1 instanceof byte[] && obj2 instanceof byte[]) {
return Arrays.equals((byte[])obj1, (byte[])obj2);
}
else if (obj1 instanceof boolean[] && obj2 instanceof boolean[]) {
...
© Stack Overflow or respective owner