首页 > 解决方案 > 如何解决不兼容类型错误?

问题描述

我尝试在数组中添加一个列表**

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

标签: javatypesincompatibletypeerror

解决方案


您正在尝试调用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));
}

然而,这并没有多大意义添加thingsthings. 也许您有一个类变量things并打算使用this关键字?

this.things.addAll(things);

推荐阅读