获取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 龙年简短祝福语有哪些