Article View: borland.public.jbuilder.java.api
Article #675Re: ArrayList.toArray() gives class cast exception
From: John McGrath TeamB
Date: Thu, 17 Apr 2003 04:23
Date: Thu, 17 Apr 2003 04:23
70 lines
3389 bytes
3389 bytes
On Wed, 16 Apr 2003 09:59:41 +0300, Ayhan Molla wrote:
> Hi all,
> The following fragment gives ClassCastException without any reason
> that I can explain.
> <I got Item class extends Object>
> ArrayList aList = new ArrayList() ;
> for(int i=0; i < 10;++i)
> aList.add( new Item() ) ;
>
> Item [] list = (Item []) aList.toArray() ; <-- this line give class cast
> exception
The reason is that the object returned by the ArrayList toArray()
method is of type Object[], which cannot be cast to Item[]. It might
be that every item in that array is of class Item, but that does not
mean that you can cast an Object[] to an Item[].
Note that the ArrayList toArray() method is overloaded:
public Object[] toArray();
public Object[] toArray(Object[] a);
The first form will construct and return an object of type Object[].
The second form will always return an object that has the same run-time
type as the argument passed to it. If the length of that argument
array is at least as large as the ArrayList, then it will copy the
elements to that array. If it is not, it will construct a new array
with the same type, fill it, and return that array.
For example, lets say you have created an ArrayList containing three
objects of class Item.
ArrayList aList = new ArrayList();
aList.add( new Item() );
aList.add( new Item() );
aList.add( new Item() );
You could create an Item[] with any of the following.
1) Item [] list = (Item[]) aList.toArray( new Item[] );
This will first create an Item[] of length 0 for the argument. The
toArray() method will then create a new Item[] of length 3 (the size
of the ArrayList) and then copy the elements from the ArrayList to that
array.
2) final static Item[] ITEM_ARRAY_PROTOTYPE = new Item[];
:
Item [] list = (Item[]) aList.toArray( ITEM_ARRAY_PROTOTYPE )
This does the same thing, but it reuses the prototype, eliminating the
extra object creation.
3) Item [] list = (Item[]) aList.toArray( new Item[ alist.size() ] );
This constructs an Item[] of the appropriate size and passes it to the
toArray() method. Since the items in aList will fit into the argument
array, toArray() will just copy the elements to that array and return
the array.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID:
<MPG.1907f64334775f0998bf58@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!news.maxwell.syr.edu!out.nntp.be!propagator2-sterling!in.nntp.be!caladan.arrakis.es!newsgroups.borland.com!not-for-mailReferences:
<3e9cff00@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:30:05 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:23:58 -0400 Message-ID: <MPG.1907f64334775f0998bf58@newsgroups.borland.com> References: <3e9cff00@newsgroups.borland.com> X-Newsreader: MicroPlanet Gravity v2.60 NNTP-Posting-Host: 151.197.11.12 X-Trace: newsgroups.borland.com 1050553437 151.197.11.12 (16 Apr 2003 21:23:57 -0700) Lines: 70 X-Authenticated-User: jpmcgrath Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!news.maxwell.syr.edu!out.nntp.be!propagator2-sterling!in.nntp.be!caladan.arrakis.es!newsgroups.borland.com!not-for-mail