嵌套泛型与通配符 [英] Nested Generics with Wildcards
本文介绍了嵌套泛型与通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么这样做:
Why does this work:
List<?> list = new LinkedList<Integer>();
,但这会导致类型不匹配错误:
while this gives a type dismatch error:
List<List<?>> list = new LinkedList<List<Integer>>();
这是为什么?
有没有办法解决这个问题,而不使用原始类型?
Why is this? Ist there a way around this, without using raw types?
推荐答案
试试
List<? extends List<?>> list = new LinkedList<List<Integer>>();
注意:您应该知道,当您使用像List这样的集合时,您只能使用它在只读模式(除了添加空值)。
Note: you should be aware that when you use a collection like List you will only be able to use it in "read only" mode (except for adding null values).
这篇关于嵌套泛型与通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文