The way to do it is this:
Integer[] array = stack.toArray(new Integer[stack.size()]);
For the record, the reason that your code doesn't compile is not just type erasure. The problem is that List.toArray()
returns an Object[]
and it has done this before generics were introduced.