Casting Object to Array type

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 ?
 
M

Michael Borgwardt

Nikolaus said:
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.

Why? What's so bad about it?

Actually, it's the only possible way, because overloaded methods are bound
at compile time.
 
C

Chris Uppal

Nikolaus said:
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 ?

No. At the JVM level there is a different bytecode to read elements of arrays
of each primitive type (with the exception of boolean[] which is treated as if
it were a byte[]). So in the end /something/ must be telling the compiler what
kind of arrays you are dealing with. Of course, it's possible that there could
be some standard function that did that test-and-switch for you, but I don't
think there is one.

-- chris
 
M

Mike Schilling

Nikolaus Rumm said:
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.
Reputation?


Is there any elegant way to solve this problem ?

No. Object[] and the 8 varieties of scalar[] are all unrelated types (e.g.
int[] is *not* a subclass or superclass of short[] ), and there's no way to
treat them generically.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top