List<String>[] stringList = new List<?>[10];
给 Type mismatch: cannot convert from List<?>[] to List<String>[]
如果我使用以下声明
List<? extends Number> inLi = new ArrayList<Integer>();
然后inLi. inLi.add(5);给出The method add(int, capture#1-of ? extends Number) in the type List<capture#1-of ? extends Number> is not applicable for the arguments (int)
1号不起作用,因为List<String>[]意味着一个String列表数组,同时List<?>[]表示任何列表的数组.在List<String>[]数组中,您不能拥有List<Integer>元素,但List<?>[]可能有一个元素List<Integer>,因此类型不匹配错误.
简而言之,在Java中,不可能像这样创建一个通用数组:
Foo<T>[] fooArray = new Foo<T>[];
Run Code Online (Sandbox Code Playgroud)
但是你可以像这样创建一个通用数组:
@SuppressWarnings("unchecked") // optional but informs the compiler
// not to generate warning message
List<String>[] stringList = new ArrayList[10];
Run Code Online (Sandbox Code Playgroud)
欲了解更多信息,请参阅本.
关于2,请看这个.
| 归档时间: |
|
| 查看次数: |
97 次 |
| 最近记录: |