N
Nikolaus Rumm
Hello,
I have to write a generic equality-check method that can handle
objects, arrays of primitives and arrays of objects. Something like...
public void isEqual(Object aFirstValue, Object aSecondValue) {
...
boolean isArray = aFirstValue.getClass().isArray();
if (isArray) {
return Arrays.equals(aFirstValue, aSecondValue); // ERROR: type
cast missing
}
}
My problem is that I have to cast the Object argument to an array type
so that the Java compiler recognizes that I don't want to call
Arrays.equals(Object), but Arrays.equals(Array1, Array2).
I could cast the argument to Object[], but that wouldn't work with
primitive objects (i.e. byte[]). Of course I could do it the stupid
way and have a big if-then-else statement for all possible primitive
arrays, but thats beyond my reputation.
Is there any elegant way to solve this problem ?
I have to write a generic equality-check method that can handle
objects, arrays of primitives and arrays of objects. Something like...
public void isEqual(Object aFirstValue, Object aSecondValue) {
...
boolean isArray = aFirstValue.getClass().isArray();
if (isArray) {
return Arrays.equals(aFirstValue, aSecondValue); // ERROR: type
cast missing
}
}
My problem is that I have to cast the Object argument to an array type
so that the Java compiler recognizes that I don't want to call
Arrays.equals(Object), but Arrays.equals(Array1, Array2).
I could cast the argument to Object[], but that wouldn't work with
primitive objects (i.e. byte[]). Of course I could do it the stupid
way and have a big if-then-else statement for all possible primitive
arrays, but thats beyond my reputation.
Is there any elegant way to solve this problem ?