java中Array的反射
java.lang.reflect包里面的类是做反射用的,其中Array是处理数组反射的,结合java.lang.Class类,可以在运行时知道数组的相关信息。
1. 判断传入的对象是不是数组
// obj is the instance passed in running timeboolean bArray = obj.getClass().isArray();
// obj is the instance passed in running timeint length = java.lang.reflect.Array.getLength(obj);
// obj is the instance passed in running timefor (int i = 0; i < len; i++) { System.out.println(Array.get(obj, i));}// obj is the instance passed in running timeClass elementType = obj.getClass().getComponentType();
// componentType - the Class object representing the component type of the new array// length - the length of the new arrayObject array = Array.newInstance(componentType, length);