How to cast/convert a Stack of Integers to an Array of doubles?
I have a Stack
of Integer
s. I need开发者_如何学编程 an array
of double
s.
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...
精彩评论