Object o = new Long[0]
System.out.println( o.getClass().isArray() )
System.out.println( o.getClass().getName() )
Class ofArray = ???
运行前 3 行发出;
true
[Ljava.lang.Long;
我怎么得到???要长吗?我可以解析字符串并执行 Class.forname(),但这太糟糕了。有什么简单的方法?
最佳答案
随便写
Class ofArray = o.getClass().getComponentType();
来自 the JavaDoc :
public Class<?> getComponentType()
Returns the
Class
representing the component type of an array. If this class does not represent an array class this method returnsnull
.
关于java - 在Java中,如何动态确定数组的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/212805/