获取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 好听的元旦简短祝福语