Java.lang.Class.getComponentType() 方法
描述
java.lang.Class.getComponentType() 方法返回代表数组组件类型的Class。 如果此类不表示数组类,则此方法返回 null。
声明
以下是 java.lang.Class.getComponentType() 方法的声明。
public Class<?> getComponentType()
参数
NA
返回值
如果这个类是一个数组,这个方法返回代表这个类的组件类型的Class。
异常
NA
示例
下面的例子展示了 java.lang.Class.getComponentType() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class ClassDemo {
public static void main(String[] args) {
String[] arr = new String[] {"admin"};
// returns the Class representing the component type
Class arrClass = arr.getClass();
Class componentType = arrClass.getComponentType();
if (componentType != null) {
System.out.println("ComponentType = " + componentType.getName());
} else {
System.out.println("ComponentType is null");
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
ComponentType = java.lang.String

