I defined List<Integer> stack = new ArrayList<Integer>();
When I'm trying to convert it to an array in the following way:
Integer[] array= stack.toArray();
I get this exception:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from Object[] to Integer[].
Why? It is exactly the same type- Integer to Integer. It's not like in this generic case when the classes are father-and-son relation
I tried to do casting:
Integer[] array= (Integer[]) stack.toArray();
But here I get this error:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
What is the problem?