An array of generic sets

Discussion in 'Java' started by Christoph, Jan 21, 2008.

  1. Christoph

    Christoph Guest

    The following command is syntactically invalid according to the Java
    compiler:

    Set<Item>[][] itemSets = new HashSet<Item>[4][2];

    (The error is "Cannot create a generic array of HashSet<Item>")

    Is there a correct way to accomplish this? As clear from the code, I
    need a 4x2 array of Sets, each of which is restricted to instances of
    Item.

    --
    Christoph
    Christoph, Jan 21, 2008
    #1
    1. Advertising

  2. Christoph schreef:
    > The following command is syntactically invalid according to the Java
    > compiler:
    >
    > Set<Item>[][] itemSets = new HashSet<Item>[4][2];
    >
    > (The error is "Cannot create a generic array of HashSet<Item>")
    >
    > Is there a correct way to accomplish this? As clear from the code, I
    > need a 4x2 array of Sets, each of which is restricted to instances of
    > Item.


    Generics and arrays do not go well together. See
    http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html.

    Use List instead:

    List<List<Set<Item>>> itemSets = new ArrayList<List<Set<Item>>>();

    and make sure to initialize intermediate lists and sets.

    If you want to use arrays anyway, you have to use an unchecked
    conversion (again, see above reference for the reason why):

    Set<Item>[][] itemSets = new HashSet[4][2];

    This will cause a warning message which you can suppress with
    @SuppressWarnings("unchecked").

    HTH, H.
    --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    http://aouw.org
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.4-svn0 (GNU/Linux)
    Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

    iD8DBQFHlHrse+7xMGD3itQRAlNmAJ0b6MpqCElPeZmCuv8vHf/RGtSQYwCaAkuu
    ZnFvuw2flbcHjb+X+5w5scc=
    =dh3y
    -----END PGP SIGNATURE-----
    Hendrik Maryns, Jan 21, 2008
    #2
    1. Advertising

  3. Christoph

    Christoph Guest

    This URL has proved very informative:
    http://java.sun.com/docs/books/tutorial/extra/generics/fineprint.html

    "Type variables don't exist at run time. [...] The component type of
    an array object may not be a type variable or a parameterized type,
    unless it is an (unbounded) wildcard type.You can declare array types
    whose element type is a type variable or a parameterized type, but not
    array objects."

    So this is the best I can do, really:

    Set<Item>[][] itemSets = new HashSet[4][2];
    Christoph, Jan 21, 2008
    #3
  4. Christoph

    Christoph Guest

    Whooops. I found the answer without seeing it had already been posted.
    Thanks! :)

    --Chris
    Christoph, Jan 21, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murat Tasan
    Replies:
    1
    Views:
    8,007
    Chaitanya
    Feb 3, 2009
  2. Mike Wahler

    Sets - Bit Array Vs. List

    Mike Wahler, Jul 31, 2003, in forum: C++
    Replies:
    3
    Views:
    2,268
    Sonoman
    Aug 2, 2003
  3. Replies:
    2
    Views:
    407
  4. Phil
    Replies:
    5
    Views:
    1,949
  5. minlearn
    Replies:
    2
    Views:
    435
    red floyd
    Mar 13, 2009
Loading...

Share This Page