|
Using reflection to check array type and length
import java.lang.reflect.Array;
public class ArrayReflection {
public static void main(String args[]) {
String data[] = new String[3];
data[0] = "Java";
printType(data);
}
private static void printType(Object object) {
Class type = object.getClass();
if (type.isArray()) {
Class dataType = type.getComponentType();
System.out.println("Array of: " + dataType);
System.out.println(" Length: " + Array.getLength(object));
}
}
}
__________________
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on July 27, 2008)
|