Java nested generic type mismatch

前端 未结 5 509
耶瑟儿~
耶瑟儿~ 2020-12-05 11:13

In the following example:

public static void main(String[] args) {

    List b = new ArrayList();
    first(b);
    second(b);

          


        
5条回答
  •  星月不相逢
    2020-12-05 11:43

    List> == List {                 //That contains any unknown type lists
                            List,
                            List,
                            List
                          }
    
    
    

    Where as

    List == List {       //That contains same unknown type lists
                            List,
                            List,
                            List
                          }
    

    So here

     List> == List {        //That contains same String lists
                            List,
                            List,
                            List
                          }
    

    Hence List is super type of List> and assignable.

    So valid value to call your fourth method is below.

        List> a1 =  new ArrayList>();
        a1.add(new ArrayList());
        a1.add(new ArrayList());
        a1.add(new ArrayList());
    
        

    提交回复
    热议问题