在 Java 中获取数组对象的组件类型
要获取 Java 中数组对象的组件类型,我们使用 getComponentType() 方法。getComponentType() 方法返回表示数组组件类型的类。如果该类不是数组类,则此方法返回 null。
声明 − java.lang.Class.getComponentType() 方法声明如下 -
public Class<?> getComponentType()
让我们看看一个在 Java 中获取数组对象的组件类型的程序 -
示例
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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP