Results 1 to 7 of 7
  1. #1
    beezerbutt is offline Member
    Join Date
    Feb 2009
    Location
    South Africa
    Posts
    18
    Rep Power
    0

    Unhappy Generic Collections---Dysfuntional Java

    Hi

    Can anyone please tell me why this code does not compile
    on the 3 lines where i am adding Strings to the collection?

    I think Generics and Collections in Java although good in their goal of achieving type safety, confuse the heck out of the newbie!!!

    Thanks in advance:

    package com.psg.cert.ch3.wrapper;

    import java.util.Arrays;
    import java.util.List;
    import java.util.ArrayList;
    import java.util.Vector;

    public class BoolTest {

    static List<?> aList=new Vector<String>();

    /**
    * @param args
    */
    public static void main(String[] args) {
    aList.add("USA");
    aList.add("Russia");
    aList.add("UK");
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm curious, why List<?> and not List<String> here?

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    It doesn't compile because the compiler does not know what is in the list, because it is declared as List<?>, and because of that, you cannot add elements to it. You have to change it to List<String> for it to work.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    beezerbutt is offline Member
    Join Date
    Feb 2009
    Location
    South Africa
    Posts
    18
    Rep Power
    0

    Default

    Hi Guys

    Thanks for the replies.
    I just want to understand the usage of "?".
    i.e. what context i would use it in...for method parameters only?

    Also, let's say that i want a Collection that is heterogeneous...
    How would i achieve that with the new Generic Collections.
    I know the point is to have type safety, but for argument sakes, how would i get it done?
    This should answer the question: Why "?" and not "String"....

    regards
    beez
    Last edited by beezerbutt; 06-28-2009 at 06:44 PM.

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    You rarely really want to use <?>, except occasionally in method parameters. <?> is roughly equivalent to <? extends Object>.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by beezerbutt View Post
    Also, let's say that i want a Collection that is heterogeneous...
    You really don't want this. First of all, it's a bad idea, one that will often cause bad bugs later; second it goes against OOP principles; lastly, why use generics here if you are trying to do this? Why even worry about "type safety" when the collection you are trying to create has no type?
    How would i achieve that with the new Generic Collections.
    I know the point is to have type safety, but for argument sakes, how would i get it done?
    By declaring your List as a List<String>

  7. #7
    beezerbutt is offline Member
    Join Date
    Feb 2009
    Location
    South Africa
    Posts
    18
    Rep Power
    0

    Default

    OKay...great thanks to you both.

    I re-read the Certification book and Add is disallowed with "?"...i had somehow missed this.
    Also, the cert book says that "?" and "? extends Object" are identical...just an FYI.

    I will mark the thread as SOLVED...

    best regards
    beez

Similar Threads

  1. Collections Help
    By Dr Gonzo in forum New To Java
    Replies: 0
    Last Post: 12-07-2008, 09:15 PM
  2. Collections Sort
    By senthil_jr in forum Advanced Java
    Replies: 2
    Last Post: 06-04-2008, 08:11 AM
  3. Collections framework
    By yuvi461 in forum New To Java
    Replies: 2
    Last Post: 01-08-2008, 10:46 AM
  4. Performance Of Collections
    By thomasprabu in forum Advanced Java
    Replies: 0
    Last Post: 01-05-2008, 11:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •