• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Generic method

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey, guis ,what is wrong with these code

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


because you are using List<? extends String> list in the method signature it means you cannot add any thing to the collection reffered to by 'list'.

To be able to add you should write either::
List <? super String> list--->this will add either String or an Object

OR

List<String> list---> this will add only String.
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be able to add you should write either::
List <? super String> list--->this will add either String or an Object


No you can add only String you cant add any thing else

Thanks
[ August 03, 2007: Message edited by: Subu Mhathma ]
 
Mike Anakin
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference between

List<? extends String>

List<? super String>
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Anakin:
List<? extends String>



Says: This will take a list of Strings, or lists of or something that "is a" String (i.e. a subclass - as String is not an interface). Thus you can retrieve Strings from this list and be confident that they have all "String" behaviour (what you retrieve may be a String or any object lower in the inheritance hierarchy).

However, I (the compiler) will not let you add anything to this list, because you are not allowed to mix generic classes and I (the compiler) can't be sure that you weren't passed a "List of sub-class to String" and put a String in it.

So, you are accepting List<String>, or a List<Subclass of String> and accessing items knowing that they have String behaviour.

List<? super String>



Says: I'm expecting a list of String or anything higher up the inheritance tree (e.g. an Object). Now the compiler can be sure that if you add a "String" to the list, any list accessors will not be confused (as the objects retrieved will have the necessary behaviour.

So, you are accepting a List<Object>, or a List<String>. In either case a "String" can be added to the list. As when something else accesses the lists items (e.g. Objects from the List<Object> ) what is in the list definitely has the "Object" behaviour.

HTH.

[ August 03, 2007: Message edited by: S J Martin ]
[ August 03, 2007: Message edited by: S J Martin ]
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic