获取Java中数组对象的组件类型
为了获取Java中的ArrayObject的组件类型,我们使用getComponentType()方法。getComponentType()方法返回表示数组的组件类型的Class。如果该类不是数组类,则此方法返回null。
声明-java.lang.Class.getComponentType()方法的声明如下-
public Class<?> getComponentType()
让我们看一个程序,以获取Java中Array对象的组件类型-
示例
public class Example {
public static void main(String[] args) {
int[] array = new int[] {1,2,3};
// obtain the Class of the component type
Class arrayClass = array.getClass();
// obtaining the component type
Class component = arrayClass.getComponentType();
if (component != null) {
System.out.println("Component type is " + component.getName());
} else {
System.out.println("Component type is null");
}
}
}输出结果
Component type is int
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短