有一件事是:
java.lang.reflect.Array.newInstance(componentType, 0).getClass();
但它创建了一个不必要的实例。
Btw,这似乎工作:
Class clazz = Class.forName("[L" + componentType.getName() + ";");
这里是测试。它输出true:
Integer[] ar = new Integer[1];
Class componentType = ar.getClass().getComponentType();
Class clazz = Class.forName("[L" + componentType.getName() + ";");
System.out.println(clazz == ar.getClass());
If this class object represents a class of arrays, then the internal form of the name consists of the name of the element type preceded by one or more ‘[‘ characters representing the depth of the array nesting.
Class.forName(..)方法不会直接为基元工作 – 对于他们,你必须创建一个名称(int)和数组简写之间的映射 – (I)