Just Curious...
Why can't i initialize a generic array like this?
private E[] data = new E[10];
This creates a compiler error saying that i can't intialize generic
arrays
The only workaround i've seen (the Collections class do this too) is
this:
private E[] data = (E[]) new Object[10];
so the question is...
Why Didn't The Creators Of Java Allow For Generic Array Intialization?
On Sun, 01 Jan 2006 19:38:55 -0800, res7cxbi wrote:
> Just Curious...
>
[quoted text clipped - 12 lines]
>
> Why Didn't The Creators Of Java Allow For Generic Array Intialization?
Because the instruction new E[] would have to become new Object[] (or
whatever) "below the cover", which means that any Object reference could
be stored. While this was once deemed appropriate (since Object [] can be
downcast to any specific array type), it is no longer considered valid.
So, for now we will have to explicitly agree to store any and all kinds of
Objects (new Object[]), and create a more specific view (cast to E[]) of
it. What is annoying is that this technique will still generate a warning
which can only be handled with a @SuppressWarning annotation (if it is
supported by your compiler).

Signature
You can't run away forever,
But there's nothing wrong with getting a good head start.
--- Jim Steinman, "Rock and Roll Dreams Come Through"