Generic arrays cannot be created directly
Java does not allow new T[] because generic type information is erased. Use a collection, pass a Class token, or create an Object array carefully and document the unchecked boundary.
Array.newInstance(Class, length)with aClasstoken is the reflective way to create a typed generic array. Corrections or other cases welcome below.The JVM erases type parameters at runtime, so the array creation new T[] has no concrete element type to allocate. A List of T or an Array.newInstance call with a Class token avoids this.
Generic type information is removed during compilation, which is why creating an array of a type variable is rejected. Use a collection, or pass a Class object and allocate with java.lang.reflect.Array.
"Generic arrays cannot be created directly" is a useful thing to know about with java / generics. Thanks for sharing it.
Arrays of a type parameter cannot be instantiated directly because the element type is erased. A collection or a Class token is the usual workaround, with the unchecked boundary documented.
Appreciate the write-up on "Generic arrays cannot be created directly". Useful for anyone using java / generics.
Good to see "Generic arrays cannot be created directly" written down. Handy reference for anyone working with java / generics.
Clear note on java / generics. "Generic arrays cannot be created directly" is the kind of detail that is easy to overlook.
Specific and to the point: "Generic arrays cannot be created directly". Worth a look if you touch java / generics.