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;
}
If you do not want to make the switch for all primitive types you could do like this:
if (ofArray.isPrimitive()) {
int length = Array.getLength(o);
for (int i = 0; i < length; i++) {
Object obj = Array.get(o, i);
System.out.println(obj);
}
}
else {
Object[] objects = (Object[]) o;
for (Object obj : objects) {
System.out.println(obj);
}
}
Edit: If you don't want to loop in two different places in your code use this method to convert the primitive arrays to an object array:
static Object[] convertToObjectArray(Object array) {
Class ofArray = array.getClass().getComponentType();
if (ofArray.isPrimitive()) {
List ar = new ArrayList();
int length = Array.getLength(array);
for (int i = 0; i < length; i++) {
ar.add(Array.get(array, i));
}
return ar.toArray();
}
else {
return (Object[]) array;
}
}
This will work for arrays with elements that are sublcasses of Object
:
if (clazz.isArray()) {
Object[] objects = (Object[]) o;
for (Object obj : objects)
System.out.println(obj);
}
If you need to cast the array to an specific array type you could (ab)use instanceof
but for, say, just printing the contents as strings, an Object[]
suffices.
UPDATE
If the array is of primitive types, you have no option but to test for each one and cast to the correct type, Object[]
won't work for this case - but there aren't that many primitive types in Java, so it won't be a gigantic switch :) . This is what I mean:
if (clazz.isArray()) {
if (clazz.getComponentType().equals(double.class)) {
double[] numbers = (double[]) o;
for (double d : numbers)
System.out.println(d);
}
}
You cannot type cast an Array of primitives to an Object[]
if (clazz == Array.class) {
Class ofArray = o.getClass().getComponentType();
// String arrayType = ofArray.getName(); // 'Double' for my test case
if (ofArray instanceof double.class)
double[] doubles = (double[]) o;
for (double d: doubles)
System.out.println(d);
}
To create an Object[]
without instanceof
checks
if (!(o instanceof Object[])) {
int len = Array.getLength(o);
Object[] objects = new Object[len];
for (int i = 0; i < len; i++)
objects[i] = Array.get (o, i);
for (Object obj: objects)
System.out.println(obj);
}
It's not enough to know your Object
is an array - you should also know the underlying type of the array elements. If your array elements are of primitive type, say double
, you need to
double[] doubleArray = (double[]) o;
This won't work:
Object[] objects = (Object[]) o;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.