开发者

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

开发者 https://www.devze.com 2023-02-17 11:59 出处:网络
I have a Stack of Integers. I need开发者_如何学编程 an array of doubles. I know stack has Stack#toArray, but this returns an Object array.

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

精彩评论

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