当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Java Java.lang.Class.getComponentType()用法及代码示例


描述

这个java.lang.Class.getComponentType() 方法返回表示数组组件类型的类。如果此类不代表数组类,则此方法返回 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

相关用法


注:本文由纯净天空筛选整理自 Java.lang.Class.getComponentType() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。