• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class cast question  RSS feed

 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Concider the following code snippets.

This causes a ClassCastException:

and this one runs smoothly:

(amapList is an ArrayList containing one or more AttributeMappingRuleTO)

I'd think that, since the objects contained in amapList is of the type I'm casting to, the first code should work.
Why does it not?
[ December 05, 2005: Message edited by: �dne Brunborg ]
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is definitely a frustrating one, and one I get annoyed with myself at times.

Recall that a Collection can contain objects of vastly different types. There's no 'general' rule that objects in a Collection must all be of the exact same type. So when toArray() is being calling, it allocates a typed array Object[] which has no sub-classes defined. Therefore casting it to any form such as (AttributeMappingRuleTO[]), will not work.

Keep in mind, there are two object types being defined here, the type for the objects in the array (which can vary), and the type of object of the array itself. Without clear direction from the developer as to what the array's object type is, the runtime system has no choice but to declare it as Object[] which cannot be cast down even though it may contain elements that all belong to the same type.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I think of this is: The toArray() method can't see your cast so you have to tell it what kind of array to make. It returns the array you pass in if it's big enough or makes a new one of the same type if it's not big enough.
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
The way I think of this is: The toArray() method can't see your cast so you have to tell it what kind of array to make. It returns the array you pass in if it's big enough or makes a new one of the same type if it's not big enough.


Doesn't "MyClass[] myArray = (MyClass[]) list.toArray(new MyClass[size]);" seem excessive though? I've written the code to do this multiple times and every time I think to myself, "wow its really irritating to be listing the class 3 times".
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Selikoff:


Doesn't "MyClass[] myArray = (MyClass[]) list.toArray(new MyClass[size]);" seem excessive though?


Yep -- although in Tiger, it's only two:

MyClass[] myArray = list.toArray(new MyClass[list.size()]);

works just fine. I can't say why toArray() with no arguments doesn't return T[] instead of Object[] in Tiger.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darn backward compatibility
I wish I could either write the following in 1.5, or that they introduced
an additional method with return type T[]:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!