-
List<String>[] stringList = new List<?>[10];
givesType mismatch: cannot convert from List<?>[] to List<String>[] -
If i use following statement
List<? extends Number> inLi = new ArrayList<Integer>();
theninLi. inLi.add(5);givesThe method add(int, capture#1-of ? extends Number) in the type List<capture#1-of ? extends Number> is not applicable for the arguments (int)
List<String>[] stringList = new List<?>[10]; gives Type mismatch: cannot convert from List<?>[] to List<String>[]
Share
No. 1 doesn’t work, because
List<String>[]means an array of String lists, whileList<?>[]means an array of lists of anything. In aList<String>[]array, you can not have aList<Integer>element, butList<?>[]could have an element that’s aList<Integer>, hence the type mismatch error.In short, in Java it is not possible to create a generic array like this:
But you can create a generic array like this:
For more information see this.
Regarding 2, see this.