This content has been marked as final. Show 2 replies
Short answer: no way. You may instanciate generic arrays with java.lang.reflect.Array.newInstance(Class<?>, int) method. However you want an array of where the component type is a type parameter, unknown at runtime, so you cannot get an appropriate Class instance.
I would suggest you create a NodeMatrix<N extends Node> class where the N field is private and define the matrix methods you will need. These methods will yield unchecked warning but at least you have type-unsafe operations gathered in a single class. The array privacy compensates type-unsafety a bit and it will be easier to debug.
I tried using a class literal - Class<N>.class - in the argument list to provide the runtime type, but then I couldn't figure out how to construct it. clazz.newInstance() wouldn't. IIRC, arrays aren't real classes. Maybe .Array.newInstance would work. Maybe create a single dimension array, then use the class of that object to feed newInstance? Probably run into the same problem. I'll have to try it.
I think the matrix class is overkill for this application, but I appreciate the idea.
I guess in the end I may just have to live with the cast and warning. Thanks.