• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Class cast question

 
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 ]
 
author
Posts: 4107
28
Google Web Toolkit Eclipse IDE Flex
  • 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.
 
(instanceof Sidekick)
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
Posts: 4107
28
Google Web Toolkit Eclipse IDE Flex
  • 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".
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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.
 
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[]:
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!