On Friday 08 December 2006 20:08, Raffael Herzog wrote:
> Niclas Hedhman schrieb:
> > Anyone knows why this doesn't work??
> >
> >    ArrayList<String>[] array = new ArrayList<String>[0];
>
> Simply put: That's because arrays are covariant. 

Well, I don't understand what that means... :o(

> Let's extend your 
> example:
>
>   Object[] objArray = array;
>
> This one's OK, ArrayList<String> is a subtype of Object, therefore,
> ArrayList<String>[] is a subtype of Object[].
>
>   ArrayList<Integer> intList = new ArrayList<Integer>();
>   intList.add(42);
>   objArray[0] = intList;

> Still OK ...
>
>   String s = array[0].get(0);
> => ClassCastException.

Huh?? I think you lost me big time...

In the original
   ArrayList<String>[] array = new ArrayList<String>[0];

Isn't it pretty obvious that only ArrayList<String> elements can be contained 
in the array??

The fact that you can assign it to an Object[] is pretty irrelevant, isn't it, 
just like it is possible to do;

   ArrayList array = new ArrayList<String>();
   array.add(42);


I must be really dumb...

Cheers
Niclas

_______________________________________________
general mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/general

Reply via email to