It is a very good question. The answer is that generics are also called "erasures." It is not just a name. The information coded by generics is used at compile time only and then is removed. So, JVM even does not know this generic type E
, so it cannot create array E[]
.
Other method toArray(T[] a)
receives the information about the type from the argument at runtime. This is the reason this method's prototype is <T> T[] toArray(T[] a)
: it gets array of type T and can return array of type T. The type is passed as a parameter.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…