Java-lang-class-getcomponenttype
提供:Dev Guides
Java.lang.Class.getComponentType()メソッド
説明
- java.lang.Class.getComponentType()*メソッドは、配列のコンポーネントタイプを表すクラスを返します。 このクラスが配列クラスを表していない場合、このメソッドはnullを返します。
宣言
以下は* java.lang.Class.getComponentType()*メソッドの宣言です
public Class<?> getComponentType()
パラメーター
NA
戻り値
このクラスが配列の場合、このメソッドはこのクラスのコンポーネントタイプを表すクラスを返します。
例外
NA
例
次の例は、java.lang.Class.getComponentType()メソッドの使用方法を示しています。
package com.finddevguides;
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