0

我读到不能将 int 数组转换为 double[] 的地方:Casting Don't Work int[] to double[], 17 Oct

我遇到了一个类似的障碍,试图将双精度数组对象转换为双精度 []。我怀疑这是同一个问题。

我想更好地理解限制。这是对所有集合的一般限制,还是仅限于数组?限制是否存在根本原因,还是只是必须解决的问题?

我有一个类可以创建一个代表输入信号的双精度数组。我将该类的一个实例传递给一个类以计算信号的 FFT。我正在使用的 FFT 类需要一个 double[] 参数。由于我将输入数组创建为 double[],看来我应该能够将其转换为这样。

4

2 回答 2

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.

于 2013-10-22T00:53:48.277 回答
0

问题在这里解释了为什么协变和逆变不支持值类型。协变和逆变不适用于值类型。但是,您可以使用 Will 提到的 linq 强制转换。

于 2013-10-21T14:53:22.643 回答