aspose file tools
The moose likes Java in General and the fly likes Class cast question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Class cast question" Watch "Class cast question" New topic
Author

Class cast question

Ådne Brunborg
Ranch Hand

Joined: Aug 05, 2005
Posts: 208
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 ]

Entia non sunt multiplicanda praeter necessitatem
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3666
    
    2

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.


My Blog: Down Home Country Coding with Scott Selikoff
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3666
    
    2

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
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

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.


[Jess in Action][AskingGoodQuestions]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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[]:


There is no emoticon for what I am feeling!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Class cast question
 
Similar Threads
Hello a collection problem
Convert decimal value to binary
Array Declaration
String - remove more than one space between words
Fractals