Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Casting issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting issue" Watch "Casting issue" New topic
Author

Casting issue

Fintan Quill
Greenhorn

Joined: Feb 28, 2006
Posts: 8
Hello,

I have an Object,x, which contains an Object array within it.

I do the following:
Object[] y = (Object[]) x;

This extracts the Object array, y.

Within this object array there are 2 String arrays & 1 double array.
This double array has a single element.

I then try the following
Object[] z = (Object[]) y[2];//where y[2] is the double array

I get the following error: java.lang.ClassCastException: [D

Can anybody advise how I can cast this double array to an Object array. I have already tried using the Wrapper class without joy.

Regards,
Fintan.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

An array of double is not an array of Objects, because doubles aren't Objects. The only way to convert an array of double into an array of Double (the wrapper class) is to create an entirely new array of the right size and then, in a loop, create a Double for each double value and store it in the array.

Having an array which itself contains other arrays of different kinds is Highly Unusual, and I suspect rather strongly that you could avoid the whole question by improving your design. For example, support the Object[] were replaced by an instance of a class of your own devising which had two String[]s and a double[] as members? That class could have a getStringArray1(), getStringArray2(), and getDoubleArray() methods which returned those members, and all the code would be much easier to follow because the confusing casts were not necessary. You could even use vastly more informative names for these methods -- I only used these silly names because I don't know what your data represents.


[Jess in Action][AskingGoodQuestions]
Fintan Quill
Greenhorn

Joined: Feb 28, 2006
Posts: 8
Thank you.

I am wondering is there any way I can "uncast" y[2] (the Object containing the double array) so that I can just access the double array itself? This would solve everything for me.

Interestingly when the double array contains more than one element the existing code works fine as it is initially fed in as an Object array as opposed to a double array.

Fintan.
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
If you know that y[2] contains a double[], then you can just do
double[] z = (double[])y[2];

Interestingly when the double array contains more than one element the existing code works fine as it is initially fed in as an Object array as opposed to a double array.

And in this case you'd get a class cast exception, since Object[] cannot be cast to double[]. You have to choose - either always make it a double array, or an Object array.

Yuriy
[ April 21, 2006: Message edited by: Yuriy Zilbergleyt ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Zintan, I strongly agree with Ernest - what you are doing sounds like it could be much better done with a different design. If you'd like to explore that, please tell us more about what you are trying to accomplish...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting issue