gpt4 book ai didi

Java

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

我有以下 Java 代码:

List<? extends Collection<String>> attributes = new ArrayList<HashSet<String>>(nrAttributes);

...

attributes.replaceAll((in) -> {
List<String> out = new ArrayList<>();
out.addAll(in);
return out;
});

但它给了我一个编译器异常(最后一个是红色的)说:

类型不匹配:无法从 List 转换为 capture#2-of ?扩展集合

最佳答案

您的扩展只是确保您将阅读 Collection<String>从你的列表中。但是你不能写它。

要获得很好的解释,请参阅此 link我从哪里获取了以下内容:

You can't add any object to List<? extends T> because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object is allowed in that List. The only "guarantee" is that you can only read from it and you'll get a T or subclass of T.

关于Java <?扩展集合>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37613510/

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