java - 如何解决不兼容类型错误?
问题描述
我尝试在数组中添加一个列表**
public Wrap(String name, Wrap wrap, List<Things> things) {
super(name);
this.bread = bread;
**things.addAll( Arrays.asList( things ) );**
}
**
我得到这个错误:不兼容的类型。必需Collection<? extends topping>
但“asList”被推断为List<T>
:不存在类型变量的实例,因此List<topping>
符合 Topping
解决方案
您正在尝试调用Arrays.asList()
,things
这已经是List
. 您可以简单地addAll()
直接调用things
:
public Wrap(String name, Wrap wrap, List<Things> things) {
super(name);
this.bread = bread;
things.addAll(new ArrayList(things));
}
然而,这并没有多大意义添加things
到things
. 也许您有一个类变量things
并打算使用this
关键字?
this.things.addAll(things);
推荐阅读
- c++ - 我什么时候应该使用新的智能指针
- javascript - How to display JSON data on click with JavaScript
- c# - 向谷歌驱动器发送 C# 分段上传请求
- python - 如何获取数据框中的所有唯一词
- php - php.ini 没有结果(php 5.6,FPM nginx,1and1 plesk)
- python - 计算字典中 SPECIFIC/CERTAIN 值的数量
- javascript - 使用嵌套文档查找查询会引发语法错误
- kubernetes - 没有选择器的服务上的 Kubernetes Nginx 入口控制器
- c# - RavenDB 客户端受限?
- python - 将窗口标题应用于python中的GUI