| Author |
getting Class cast exception
|
Maan Suraj
Ranch Hand
Joined: Dec 20, 2007
Posts: 289
|
|
I get a class cast excpetion at line Integer []arr=(Integer[])arr. Acc to my understanding, arr is of type Object, and i try to downcast it to Integer[]. But why does it throw Class Cast Exception
|
Man Suraj
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
|
 |
Jose Ayerdis
Ranch Hand
Joined: Sep 14, 2008
Posts: 30
|
|
i believe you meant
and the answer to your question is simple. in your code you have this HashSet wich add a series of Integer but the hasset it self just contains a bunch of Objects.
Second you instanciate a List named list with the previos set collections and called to the method toArray() wich always return Object[] then you try to cast but you can't beause the Object[] Array has nothing to do with the Integer[] array is just like saying Animal[] cast to Integer[] they have nothing.
|
 |
Maan Suraj
Ranch Hand
Joined: Dec 20, 2007
Posts: 289
|
|
Thanks Jose, but i dont understand one thing....
Second you instanciate a List named list with the previos set collections and called to the method toArray() wich always return Object[] then you try to cast but you can't beause the Object[] Array has nothing to do with the Integer[] array is just like saying Animal[] cast to Integer[] they have nothing.
As far as I Understand, Object is the SuperClass/ Parent Class for all objects. Interger[] is a subclass for Object[] , so i thought downcasting is possible and should not throw any error(Unlike the case of Animal[] and Integer[] you are talking about). Could you please explain this?
|
 |
Jose Ayerdis
Ranch Hand
Joined: Sep 14, 2008
Posts: 30
|
|
let me put it clear you are kind of doing this
Here you can se it clearly that you cannot cast an object to an integer array
Because after all thats what you retrieve from your HashSet
|
 |
Maan Suraj
Ranch Hand
Joined: Dec 20, 2007
Posts: 289
|
|
|
Ok.. Thanks Jose... Got it .....:-)
|
 |
 |
|
|
subject: getting Class cast exception
|
|
|