Why does JAVA not allow the creation of generic arrays?
First of all, I think that those who customize java standards can allow java to create generic arrays. They just weigh them and think they are still prohibited. Let's talk about my speculation:
If we write the following code, there is no problem:
List
a = new ArrayList
();
So why can't we get an array:
List
[] arr = new ArrayList
[10];
Let's take a look at the differences between the array variable and the common variable: the array variable arr and the common variable a are both in the stack, but that's it! Arr [0] is in the heap. So? So we have the following example:
List
[] arr = new ArrayList
[10];Object[] orr = arr;List
buf = new ArrayList
();orr[0] = buf;List
str = arr[0];String val = str.getValue();
The first element in the heap is List. But arr [0] still points to it. If it is a common variable, this is impossible. Therefore, it is not safe even if generic is introduced. The last sentence, the compiler will add the string Conversion, causing ClassCastException. The generic type is intended for security. If the array security cannot be guaranteed, why should we do so?