java:获取数组组件的类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4737598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:48:17  来源:igfitidea点击:

java: getting the class of the components of an array

javaarraysgenerics

提问by Jason S

If I have

如果我有

public <T> doSomething(T[] array)
{
}

how can I get T.classfrom array?

我怎样才能T.classarray

If I do array.getClass()that gets me T[].classinstead.

如果我这样做array.getClass()T[].class反而会得到我。

回答by Sean Patrick Floyd

Component Type

组件类型

Use this:

用这个:

array.getClass().getComponentType()

Returns the Classrepresenting the component type of an array. If this class does not represent an array class this method returns null.

返回Class表示数组组件类型的 。如果此类不代表数组类,则此方法返回null

Reference:

参考:



Safe / Unsafe casting

安全/不安全铸造

Is there a way I can cast to Class from Class returned by getComponentType() without getting a compiler warning?

有没有办法可以从 getComponentType() 返回的 Class 转换为 Class 而不收到编译器警告?

take this method:

采取这种方法:

public <T> void doSomething(final T[] array) throws Exception{
    final Class<? extends Object[]> arrayClass = array.getClass();
    final Class<?> componentType = arrayClass.getComponentType();
    final T newInstance = (T) componentType.newInstance();
}

Here's the generated byte code:

这是生成的字节码:

public void doSomething(java.lang.Object[] array) throws java.lang.Exception;
     0  aload_1 [array]
     1  invokevirtual java.lang.Object.getClass() : java.lang.Class [21]
     4  astore_2 [arrayClass]
     5  aload_2 [arrayClass]
     6  invokevirtual java.lang.Class.getComponentType() : java.lang.Class [25]
     9  astore_3 [componentType]
    10  aload_3 [componentType]
    11  invokevirtual java.lang.Class.newInstance() : java.lang.Object [30]
    14  astore 4 [newInstance]
    16  return

As you can see, the parameter type is erased to Object[], so the compiler has no way to know what T is. Yes, the compiler could use array.getClass().getComponentType(), but that would sometimes fail miserably because you can do stuff like this:

可以看到,参数类型被擦除为Object[],所以编译器无法知道T是什么。是的,编译器可以使用array.getClass().getComponentType(),但有时会失败,因为您可以执行以下操作:

Object[] arr = new String[] { "a", "b", "c" };
Integer[] integerArray = (Integer[]) arr;
doSomething(integerArray);

(In this case array.getClass().getComponentType()returns String.class, but Tstands for Integer. Yes, this is legal and does not generate compiler warnings.)

(在这种情况下array.getClass().getComponentType()返回String.class,但T代表Integer。是的,这是合法的,不会生成编译器警告。)

回答by James Mudd

If you want to do this for multi-dimensional arrays the following recursive code will work

如果您想对多维数组执行此操作,以下递归代码将起作用

    Class<?> getArrayType(Object array) {
        Object element = Array.get(array, 0);
        if (element.getClass().isArray()) {
            return getArrayType(element);
        } else {
            return array.getClass().getComponentType();
        }
    }