zollen
asked on
Reflection: Extracting object from an array of unknown type and unknown dimension
I am trying to use Java Reflection APIs for extracting all member items from an array of unknown type and unknown dimension?? Here are some possible inputs:
int [] arr; ---> trying to extract individual item from the int array
ABC [] arr; ---> trying to extract indidvual ABC object from the ABC array
float [] [] [] arr; ---> trying to extract individual float from a 3D float array
ABC [] [] [] arr; ----> trying to extract individual ABC object from a 3D ABC array
Any ideas??
int [] arr; ---> trying to extract individual item from the int array
ABC [] arr; ---> trying to extract indidvual ABC object from the ABC array
float [] [] [] arr; ---> trying to extract individual float from a 3D float array
ABC [] [] [] arr; ----> trying to extract individual ABC object from a 3D ABC array
Any ideas??
for multi-dimension arrays you can check if getConponentType().isArray () is true
ASKER
How do you handle arrays with *unknown* type and *unknown* dimension??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if you don't know the type you can always cast it to Object or Object[] and then invoke your desired method if you know the name of method
if(arrObj.getClass().isArray()){
Object[] array = (Object[])arrObj;
if(array.getClass().getComponentType().isArray(){
for(Object obj : array){
Object[] secondDimension = (Object[]) obj
//....
}
}
}
Consider the object you don't know its type is arrObj, now check following snippet
Open in new window