0

I read where an array of int can't be cast as double[]: Casting Don't Work int[] to double[], 17 Oct

I have run into a similar roadblock trying to cast an array Object of doubles to double[]. I suspect it is the same issue.

I would like to understand the limitation better. Is this a general limitation on all collections, or is it limited to arrays? Is there a fundamental reason for the limitation, or is it just something that has to be worked around?

I have a class that creates an array of doubles that represents an input signal. I pass an instance of that class to a class to calculate the FFT of the signal. The FFT class I'm using needs a double[] parameter. Since I create the input array as double[], it seems I should be able to cast it as such.

Community
  • 1
  • 1
Gene
  • 91
  • 1
  • 2
  • 9
  • 1
    What language are you using? Please add that to the tags for this post. – musical_coder Oct 21 '13 at 14:46
  • Implicit conversions of arrays aren't part of the specs. You can swing it with linq, however. `object[] foo = new double[]{0d}; double[] done = foo.OfType().ToArray();` –  Oct 21 '13 at 14:48
  • 1
    It is not clear to me what might be your issue. You stated that you get the `double[]` as a parameter, why do you need a cast then? – Thomas Jungblut Oct 21 '13 at 14:51
  • It's a general question; even a reference to documentation that explains the limitation on casting arrays would be helpful. – Gene Oct 21 '13 at 14:54
  • This is a first attempt at a project, (and at Java programming), so the code is pretty gnarled - I wouldn't want to inflict it on anyone. Here's some pseudo code: – Gene Oct 21 '13 at 14:57
  • Class A: loop to create double[] input signal, Class B: create FFT array from input signal using FastFourierTransformer(), Class C: create GUI, pass instance of A to B, getB, displayA, displayB – Gene Oct 21 '13 at 15:01
  • Thomas J.: The issue is the instance of the array is type ArrayClass, not double[]. FastFourierTransformer needs double[]. – Gene Oct 21 '13 at 15:13
  • Write a loop. Is it that hard?? – Hot Licks Oct 22 '13 at 01:02
  • possible duplicate of [Casting don't work (int \[\]to double\[\])](http://stackoverflow.com/questions/19438086/casting-dont-work-int-to-double) – Hot Licks Oct 22 '13 at 01:03
  • What part of the Java documentation would lead you to believe that this is possible? – Hot Licks Oct 22 '13 at 01:05

2 Answers2

1

trying to cast an array Object of doubles to double[]

There is no such thing as 'an array Object of doubles' in Java. There is double[], there is Double[], and there is Object[] where the elements are Doubles.

You can cast between the last two, in both directions if the underlying type really is Double[], but not between the first and either of the others.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

The problem is explained here Why covariance and contravariance do not support value type. Covariance and contravariance does not work with value types. However, you can force the cast by using linq as Will mentioned.

Community
  • 1
  • 1
Daniel Leiszen
  • 1,827
  • 20
  • 39