开发者

How to cast/convert a Stack of Integers to an Array of doubles?

I have a Stack of Integers. I need开发者_如何学编程 an array of doubles.

I know stack has Stack#toArray, but this returns an Object array.

How is this done?


Generally, arrays of a concrete types are obtained like that:

Integer[] array = stack.toArray(new Integer[stack.size()]);

But since you need to change the type of the array, you'd better iterate:

int i = 0;
double[] doubles = new double[stack.size()];
for (Integer value : stack) {
    doubles[i++] = value.doubleValue();
}


You can't do that unless you do it iteratively.


i think that you can cast the Object array returned by the Stack#toArray iteratively to integer...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜