Java泛型集合,无法将列表添加到列表

Java泛型集合,无法将列表添加到列表,java,generics,collections,Java,Generics,Collections,为什么会出现以下情况 public class ListBox { private Random random = new Random(); private List<? extends Collection<Object>> box; public ListBox() { box = new ArrayList<>(); } public void addTwoForks() { int sizeOne = random

为什么会出现以下情况

public class ListBox {
    private Random random = new Random();
    private List<? extends Collection<Object>> box;

public ListBox() {
    box = new ArrayList<>();
}

public void addTwoForks() {
    int sizeOne = random.nextInt(1000);
    int sizeTwo = random.nextInt(1000);

    ArrayList<Object> one = new ArrayList<>(sizeOne);
    ArrayList<Object> two = new ArrayList<>(sizeTwo);

    box.add(one);
    box.add(two);
}

public static void main(String[] args) {
    new ListBox().addTwoForks();
}
}
公共类列表框{
私有随机=新随机();

私有列表您已声明
是扩展
对象
集合
列表
。但根据Java编译器,它可以是扩展
集合
的任何内容,即
列表
。因此它必须禁止使用泛型类型参数的
添加
操作因此,它不能让您将
ArrayList
添加到可能是
List
列表中

尝试删除通配符:

private List<Collection<Object>> box;
私有列表框;

这应该是可行的,因为你当然可以将
ArrayList
添加到
Collection
列表中,所以这个答案可能会有所帮助-这是有道理的,但是如果我想添加一个向量和一个ArrayList?编辑,第一个注释中的链接解释了super;)@arynaq这两个实现列表的用法,所以你可以执行
List然后你可以同时添加向量和数组列表,尽管你只能将它们作为
列表
。如果你需要
数组列表
向量
上的特定方法,你必须测试和向下转换,或者想出一些其他的方法——但首先要确保你真的需要它们!或者,按照建议,
列表
d已适用于向量和数组列表以及集合等。
private List<Collection<Object>> box;