gpt4 book ai didi

Java 泛型和 addAll 方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:01:45 25 4
gpt4 key购买 nike

Java 集合中 addAll(..) 方法的正确参数类型是什么?如果我这样做:

List<? extends Map<String, Object[]>> currentList = new ArrayList<Map<String, Object[]>>();
Collection<HashMap<String, Object[]>> addAll = new ArrayList<HashMap<String, Object[]>>();
// add some hashmaps to the list..
currentList.addAll(addAll);

我知道我需要初始化这两个变量。但是,我收到一个编译错误(来自 eclipse):

Multiple markers at this line
- The method addAll(Collection<? extends capture#1-of ? extends Map<String,Object[]>>) in the type List<capture#1-of ? extends Map<String,Object[]>> is not applicable for the arguments (List<capture#2-of ? extends
Map<String,Object[]>>)
- The method addAll(Collection<? extends capture#1-of ? extends Map<String,Object[]>>) in the type List<capture#1-of ? extends Map<String,Object[]>> is not applicable for the arguments
(Collection<HashMap<String,Object[]>>)

我做错了什么?

最佳答案

替换extends通过 super .

addAll()本身需要一个 ? extends T参数,当 T 时那将不起作用是? extends Map<K, V> .随着变化,它将变得有效 ? extends ? super Map<K, V> .

但是,最好还是删除那个 ? extends (或 ? super )。在这种特殊情况下这是多余的。只需使用 List<Map<String, Object[]>> . addAll()然后将有效地采取 ? extends Map<String, Object[]>> .

关于Java 泛型和 addAll 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2701630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com