c.toArray might (incorrectly) not return Object[] (see 6260652)
Martin Buchholz
martinrb at google.com
Mon May 25 06:42:47 UTC 2009
I continue to believe that not fixing 6260652
was a mistake, both technically and
from the point of view of community relations.
I prefer the current implementation of ArrayList(Collection)
to one using c.toArray(ZERO_ARRAY) because it has
slightly less trust of the argument collection.
(In any case, I think it's an academic question;
it would not be worth changing
either implementation to the other for aesthetic reasons)
Historically, we have also had bugs implementing
toArray(T[]).
Martin
On Fri, May 22, 2009 at 05:05, David Holmes - Sun Microsystems <
David.Holmes at sun.com> wrote:
> Doug Lea said the following on 05/22/09 21:56:
>
>> Sorry; I should have been clearer about why
>> c.toArray(new Object[c.size()])
>> is subtly wrong here. ArrayList.size must equal
>> the number of elements, which might be different
>> than the array size. If c's size shrinks at an
>> inconvenient moment during the call, then we might
>> think that the trailing null, that toArray(T[] a)
>> is required to append if a is too big, is actually a
>> (null) element of the collection.
>>
>
> Ah I see.
>
> I'm thinking though that I'd find this hack more aesthetically pleasing:
>
> static final Object[] ZERO_ARRAY = new Object[0];
> ...
> elementData = c.toArray(ZERO_ARRAY);
>
> this deals with the size issue, gets the right type and only creates one
> array (asuming the collection doesn't concurrently grow).
>
> Cheers,
> David
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20090524/7979cb8d/attachment.html>
More information about the core-libs-dev
mailing list