I'm having problems casting an object array to a key-value pair array, with generic types for the key and value objects. Here is a minimal example.
public class Main {
public static void main(String[] args) {
array = (Map.Entry<Integer, Integer>[]) new Object[1];
}
private static Map.Entry<Integer, Integer>[] array;
}
Changing Map.Entry to a class (rather than interface) doesn't do the trick either.
Error trace:
run:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.util.Map$Entry;
at lab2.Main.main(Main.java:13)
Java Result: 1
new Object[1]is type compatibile withMap.Entry<Integer, Integer>[]? Of course a new vanillaObject[]array cannot be cast to something more specific. In the same way that this is not legal either:(String)new Object();.