Did I Miss Generic Array Creation?
HeapMember<Key,Value,Comp$gt;[] fibNodes = (HeapMember<Key,Value,Comp>[])Array.newInstance(HeapMember.class,size());
results in a waring from lint.
The first thing I tried didn't compile:
//doesn't compile
HeapMember<Key,Value,Comp>[] fibNodes = new HeapMember<Key,Value,Comp>[size()];
results in
jdigraph/v2/source/collection/net/walend/collection/FibHeap.java:57: generic array creation
HeapMember<Key,Value,Comp>[] fibNodes = new HeapMember<Key,Value,Comp>[size()];
I tried dynamically creating the array using java.lang.reflect.Array:
HeapMember<Key,Value,Comp>[] fibNodes = (HeapMember<Key,Value,Comp>[])Array.newInstance(HeapMember.class,size());
which gives me the warning from lint again.
/Users/dwalend/projects/opensource/jdigraph/v2/source/collection/net/walend/collection/FibHeap.java:58: warning: [unchecked] unchecked cast
found : java.lang.Object
required: net.walend.collection.HeapMember<Key,Value,Comp>[]
HeapMember<Key,Value,Comp>[] fibNodes = (HeapMember<Key,Value,Comp>[])Array.newInstance(HeapMember.class,size());
I've tried doing other things, especially to the HeapMember.class argument, but haven't found a solution that compiles with no warnings.
Is there some bit of API I missed? Is there a good reason not to create arrays of generics? Or should I report a RFE to Sun asking them to add generic parameters to Array.newInstance()?
Thanks,
Dave
- Login or register to post comments
- Printer-friendly version
- dwalend's blog
- 2321 reads






Comments
why not just
by kithouna - 2011-03-30 09:01
why not just do
it will still have an unchecked cast, but at least it doesn't need to use reflection