Java.lang.Class类 getComponentType()方法有什么功能呢?

转自:

Java.lang.Class类 getComponentType()方法有什么功能呢?

下文讲述Class类中的getComponentType()方法的功能,如下所示:

getComponentType()方法的功能

java.lang.Class.getComponentType()() 方法的功能
返回类对应的数组的组件类型
当类不表示数组时,则此方法返回null

getComponentType()方法的语法

语法
   public Class<?> getComponentType()
参数
  无

返回值
   如果类是一个数组,则返回类的组件类型,
 否则返回null

例:
getComponentType()方法的示例分享

 package com.java.other;
import org.junit.Test;
public class other {

	/**
	 * java265.com java.lang.Class 测试示例分享
	 */
	@Test
	public void test() {
		String[] arr = new String[] { "java265.com", "Java教程" };

		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

版权声明:本文为qq_25073223原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。