这个错误出现在我试图用一个 ArrayList<ArrayList>() new 一个 List<List> 对象的时候

1
2
List<List<Integer>> res = new LinkedList<LinkedList<Integer>>();
    }

The correct writing should be: List<List<Integer>> ret = new ArrayList<List<Integer>>(); Since in this way, you can add not only ArrayList but also LinkedList to ret

把第二个 ArrayList 改成 List ,错误就没有了,那么原理是什么呢?

经查找发现,这是一个 泛型 应用的常踩坑:

  1. 首先,如果 A is a B , 我们可以把 A 赋值给 B

    1
    2
    3
    
    Object someObject = new Object();
    Integer someInteger = new Integer(10);
    someObject = someInteger;   // OK
    

    Integer 继承自 Object,是它的一个子类型, 所以这样赋值没有问题,这是转型。

  2. Integer 也是一种 Number, Double 也是一种 Number,所以下面这样也是可以的

    1
    2
    3
    4
    
    public void someMethod(Number n) { /* ... */ }
    
    someMethod(new Integer(10));   // OK
    someMethod(new Double(10.1));   // OK
    

    也可以使用泛型

    1
    2
    3
    
    Box<Number> box = new Box<Number>();
    box.add(new Integer(10));   // OK
    box.add(new Double(10.1));  // OK
    
  3. 问题

    1
    
    public void boxTest(Box<Number> n) { /* ... */ }
    

    如果这样,我们可以传入 Box 或者 Box

    答案是否定的。

    Integer 是 Number 的子类,Double 也是 Number 的子类, 但是,Box 和 Box 都不是 Box 的子类,它们的关系是并列的,都是 Object 的子类。

    image-20211218154815369

    在java doc中

    In general, if Foo is a subtype (subclass or subinterface) of Bar, and G is some generic type declaration, it is not the case that G<Foo> is a subtype of G<Bar>. This is probably the hardest thing you need to learn about generics, because it goes against our deeply held intuitions.

    Same thing happens here it’s Bar = List<Integer> and Foo = ArrayList<Integer> as ArrayList<ArrayList<Integer>> is not sub type of List<List<Integer>>