You cannot create arrays of parameterized types, so this code in Eclipse
ArrayList<Integer>[] list = new ArrayList[1];
Can't be parameterized , but Eclipse shows a warning
Type safety: The expression of type
ArrayList[]
needs unchecked conversion to conform toArrayList<Integer>[]
And also shows suggestion Infer Generic Type Arguments which does nothing when submitted.
Infer Generic Type Arguments Replaces raw type occurrences of generic types by parameterized types after identifying all places where this replacement is possible.
Should this suggestion be removed or am I missing something?
Java allows generic classes, methods, etc. that can be declared independent of types. However, Java does not allow the array to be generic. The reason for this is that in Java, arrays contain information related to their components and this information is used to allocate memory at runtime.
Type inference represents the Java compiler's ability to look at a method invocation and its corresponding declaration to check and determine the type argument(s). The inference algorithm checks the types of the arguments and, if available, assigned type is returned.
If generic array creation were legal, then compiler generated casts would correct the program at compile time but it can fail at runtime, which violates the core fundamental system of generic types.
You can't have arrays of generic classes. Java simply doesn't support it. and then create an array of MyObjectArrayList .
Yes, the suggestion should be removed. It is not possible to replace the raw type with a parameterized type here, because an array creation expression must use a reifiable type as the component type. It's illegal to do new ArrayList<Integer>[1]
. You can only do new ArrayList[1]
or new ArrayList<?>[1]
, both of which will produce a warning in order to convert to type ArrayList<Integer>[]
(the second one will require an explicit cast which produces an unchecked cast warning).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With