🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #676

Re: ArrayList.toArray() gives class cast exception

#676
From: John McGrath TeamB
Date: Thu, 17 Apr 2003 04:45
33 lines
2208 bytes
There is one more thing I should have mentioned:

> 2)  final static Item[] ITEM_ARRAY_PROTOTYPE = new Item[];
>       :
>     Item [] list = (Item[]) aList.toArray( ITEM_ARRAY_PROTOTYPE )

> 3)  Item [] list = (Item[]) aList.toArray( new Item[ alist.size() ] );

In general, choices #2 and #3 are better than one, since you do not 
create an extra array each time you need to get the contents of your 
ArrayList.  #3 might seem a little better than #2 at first glance, 
since it requires allocating the prototype.  However that is not 
especially true.  

If your ArrayList is ever empty when you use this code, choice #3 will 
return the prototype.  It his happens more than once, you will save in 
object creation, since the same object will be used each time.  Note 
that while arrays are generally not thought of as immutable, an array 
of length 0 *is* immutable, so this is fine.

The difference is probably not significant in most cases, but if the 
ArrayList is often empty, choice #2 can avoid the creation of lots of 
objects.  At worst, it will create one extra object.

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID: <MPG.1907fb5a6a62dda398bf59@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail
References: <3e9cff00@newsgroups.borland.com> <MPG.1907f64334775f0998bf58@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,5c0e8f08630b5fc1
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2003-04-16 21:46:42 PST
From: John McGrath  [TeamB] <ng-b@JPMcGrath.net>
Newsgroups: borland.public.jbuilder.java.api
Subject: Re: ArrayList.toArray() gives class cast exception
Date: Thu, 17 Apr 2003 00:45:03 -0400
Message-ID: <MPG.1907fb5a6a62dda398bf59@newsgroups.borland.com>
References: <3e9cff00@newsgroups.borland.com> <MPG.1907f64334775f0998bf58@newsgroups.borland.com>
X-Newsreader: MicroPlanet Gravity v2.60
NNTP-Posting-Host: 151.197.11.12
X-Trace: newsgroups.borland.com 1050554702 151.197.11.12 (16 Apr 2003 21:45:02 -0700)
Lines: 33
X-Authenticated-User: jpmcgrath
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail