[Java]
Package Ref;
Import java. awt. Button;
Public class SampleComponentReflection {
Public static void main (String [] args ){
Int [] ints = new int [2];
Button [] buttons = new Button [6];
String [] [] twoDim = new String [4] [5];
String [] two = new String [4];
PrintComponentType (ints );
PrintComponentType (buttons );
PrintComponentType (twoDim );
PrintComponentType (two );
}
Static void printComponentType (Object array ){
Class arrayClass = array. getClass ();
String arrayName = arrayClass. getName ();
Class componentClass = arrayClass. getComponentType ();
String componentName = componentClass. getName ();
System. out. println ("ArrayClass:" + arrayClass + ", ---- ArrayName :"
+ ArrayName + ", ------- Component:" + componentClass );
}
}
Output result:
[Html]
ArrayClass: class [I, ---- ArrayName: [I, ------- Component: int
ArrayClass: class [Ljava. awt. Button;, ---- ArrayName: [Ljava. awt. Button;, ------- Component: class java. awt. Button
ArrayClass: class [[Ljava. lang. String;, ---- ArrayName: [[Ljava. lang. String;, ------- Component: class [Ljava. lang. String;
ArrayClass: class [Ljava. lang. String;, ---- ArrayName: [Ljava. lang. String;, ------- Component: class java. lang. String
Author: bengda