?
Solved

Reflection: Extracting object from an array of unknown type and unknown dimension

Posted on 2010-01-02
5
Medium Priority
?
535 Views
Last Modified: 2013-12-29
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??
0
Comment
Question by:zollen
  • 4
5 Comments
 
LVL 8

Expert Comment

by:mnrz
ID: 26162385
You don't need reflection.
Consider the object you don't know its type is arrObj, now check following snippet

		if(arrObj.getClass().isArray()){
			if(int.class.equals(arrObj.getClass().getComponentType())){
				int[] realArray = (int[]) arrObj;
				for(int i = 0; i < realArray.length; ++i){
					//handle array
				}
			}else if (ABC.class.equals(arrObj.getClass().getComponentType())){
				ABC[] abc = (ABC[])arrObj; 
				for(int i = 0; i < realArray.length; ++i){
					//handle array
				}
			}
		}

Open in new window

0
 
LVL 8

Expert Comment

by:mnrz
ID: 26162395
for multi-dimension arrays you can check if getConponentType().isArray() is true
0
 

Author Comment

by:zollen
ID: 26162430
How do you handle arrays with *unknown* type and *unknown* dimension??

0
 
LVL 8

Accepted Solution

by:
mnrz earned 500 total points
ID: 26162466
method arrObj.getClass().getComponentType() returns the type of each array element and arrObj.getClass().isArray() tells if the class is of type array. so if you call arrObj.getClass().getComponentType().isArray() again you understand that each type of this array is array itself (this means it is more than one dimension) you can call this recursively untill arrObj.getClass().getComponentType().isArray() returns false so the number of true conditions is number of dimensions




if (arrObj.getClass().getComponentType().isArray()){
	//more than one dimension!
}

Open in new window

0
 
LVL 8

Expert Comment

by:mnrz
ID: 26162479
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
          //.... 
       }
    }
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question