Why can not I add an object of type Object into a List<?>?

后端 未结 5 1517
忘了有多久
忘了有多久 2020-12-21 12:03

As far as I understand a List is definds as a list of some specific , yet unknown type . So whatever is the parameter type of this List

5条回答
  •  遥遥无期
    2020-12-21 12:57

    "?" is called WildCard Capture which means type parameter matches an Unknown Type. This means

     List listObj = returnSomeList(); 
    

    is a list of

     Unknown Type 
    

    and you are trying to add an Object into a List of

      Unknown Type.
    

    The example posted by you will give you compile time error.

提交回复
热议问题