So I have an Object which MIGHT be an array. It can also be primitive, or a string. If it is an array, it can be an array of literally anything.
I have no problem figuring out if it is an array but I can't seem to cast it into anything that I can iterate through to get the values out of.
// o is an object and clazz is the class of the o
if (clazz == Array.class) {
Class ofArray = o.getClass().getComponentType();
String arrayType = ofArray.getName(); // 'Double' for my test case
//ERROR: [D cannot be cast to [Ljava.lang.Object
Object[] objects = (Object[]) o;
}
My background is in ruby and php (where it would just work) and the static typing is messing with my head. Any ideas?
EDIT:
This throws the error
[D cannot be cast to [Ljava.lang.Object.
What am I missing?
if (o.getClass().isArray()) {
Object[] objects = (Object[]) o;
}