1.5.3 表示数组类型
Java 数组是 对象,但是其类型是由 JVM 在运行时创建的,每个数组都会根据其 元素类型 和 维度 创建一个 新类,Java 数组类同时实现了 Cloneable 和 java.io.Serializable 接口。
数组的 class literal 与其他类型相同,比如一个一维 Object 数组的类信息可以表示为:Object[].class,所以可以用如下代码查询 copyInto 方法:
Method m = Vector.class.getMethod("copyInto", new Class[] { Object[].class });
可以用 isArray 方法判断 Class 对象是否表示数组。
可以用 getComponentType 方法获取数组的 组件类型,对于多维数组,Java 视其为嵌套的一维数组,所以下面代码结果为 int[].class:
int[][].class.getComponentType();
注意:对于 int[][]
- 组件类型(component type):使用
getComponentType方法获取,为int[].class。 - 元素类型(element type):数组最小元素的类型,为
int.class。