Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting issue

 
Fintan Quill
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Fintan Quill
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic