Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to get base type of a multidimensional array in reflection

skidmarksJan 12 2011 — edited Jan 12 2011
If I have a Field I can get use Class<?> c = field.getType() to get it's Class but I don't know how to get the array type easily. For example:
<code>
Class cls {
float[][] flt;
}

Field[] f = cls.class.getDeclaredFields();
Class<?> c = f[0].getType;
</code>

I can get "[[F" representing the declaration in reflection, but how can I get "F" representing the primitive type float? I have the same question for a reference to a multidimensional array of classes.

The only thing that I can think of is to parse the string in c.getCanonicalName() or c.getComponentType() to find the representative type and then determine if it's a primitive or class. If it's a class then create an object of that class (<>.newInstance()) and then do something with the object. Is there an easier way?
This post has been answered by 802316 on Jan 12 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 9 2011
Added on Jan 12 2011
2 comments
227 views