This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes cannot add  values to unknown type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "cannot add  values to unknown type" Watch "cannot add  values to unknown type" New topic
Author

cannot add values to unknown type

Kasun Wixkramanayake
Ranch Hand

Joined: Feb 09, 2010
Posts: 74
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

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>.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cannot add values to unknown type
 
Similar Threads
Collection generics method types
Try to understando generic methods
wildcard 's bound as type parameter
Generic Methods
Unsafe or Unchecked Operations with class that extends ArrayList