Java Collections using wildcard

前端 未结 7 1582
天命终不由人
天命终不由人 2020-12-11 03:26
public static void main(String[] args) {

    List mylist = new ArrayList();

    mylist.add(\"Java\"); // compile error

}
         


        
      
      
      
7条回答
  •  生来不讨喜
    2020-12-11 03:50

    In his book great 'Effective Java' (Second Edition) Joshua Bloch explains what he calls the producer/consumer principle for using generics. Josh's explaination should tell you why your example does not work (compile) ...

    Chapter 5 (Generics) is freely available here: http://java.sun.com/docs/books/effective/generics.pdf

    More information about the book (and the author) are available: http://java.sun.com/docs/books/effective/

提交回复
热议问题