Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cannot add values to unknown type

 
Kasun Wixkramanayake
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15276
38
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The generic type T is defined as: T extends List<? extends Number>

So, type T is a List of some unknown type (indicated by the question mark) that extends Number. You can't add a Long to such a list, because you don't know what the unknown type is. Suppose that someone uses this on a List<Double>. Ofcourse it shouldn't be possible to add a Long to a List<Double>.

If you want to be able to add a Long, then you can change the first line to this:

 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:If you want to be able to add a Long, then you can change the first line to this:



Or if you want it a bit more general:
Since then T could be any of List<Long>, List<Number> and List<Object>.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic