Thread View: borland.public.jbuilder.java.api
5 messages
5 total messages
Started by Ayhan Molla
Wed, 16 Apr 2003 06:59
ArrayList.toArray() gives class cast exception
Author: Ayhan Molla
Date: Wed, 16 Apr 2003 06:59
Date: Wed, 16 Apr 2003 06:59
10 lines
1174 bytes
1174 bytes
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
Re: ArrayList.toArray() gives class cast exception
Author: Gillmer J. Derge [TeamB]
Date: Wed, 16 Apr 2003 13:05
Date: Wed, 16 Apr 2003 13:05
12 lines
1393 bytes
1393 bytes
"Ayhan Molla" <amolla@havelsan.com.tr> wrote in message
news:3e9cff00@newsgroups.borland.com...
> Item [] list = (Item []) aList.toArray() ; <-- this line give class cast
> exception
Because that method creates an Object array not an Item array. You need to
do something like this instead:
Item[] list = (Item[])aList.toArray(new Item[aList.size()]);
--
Gillmer J. Derge [TeamB]
Re: ArrayList.toArray() gives class cast exception
Author: Shankar Unni
Date: Wed, 16 Apr 2003 17:16
Date: Wed, 16 Apr 2003 17:16
8 lines
1506 bytes
1506 bytes
Gillmer J. Derge [TeamB] wrote: > Because that method creates an Object array not an Item array. And the reason this is done this way is that an ArrayList can contain any type of object, so there's definitely no guarantee that all the objects are of one type. Only you (the programmer) knows this, which is why you have to ask for it explicitly if you want a typed array.
Re: ArrayList.toArray() gives class cast exception
Author: 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
Re: ArrayList.toArray() gives class cast exception
Author: John McGrath TeamB
Date: Thu, 17 Apr 2003 04:45
Date: Thu, 17 Apr 2003 04:45
33 lines
2208 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads