Announcement

Collapse
No announcement yet.

Java generics

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Java generics

    I haven't used Java for several years, and when I last did 1.5 had just been released. How do I initialize an array of generics? Eclipse complains about this:

    E[] list;
    ...
    list = new E[size];

  • #2
    Haven't played with java either for a while, briefly introduced myself to 1.5 (or 5.0.. whatever)..

    Does this help to work it around? Saw this somewhere..


    E[] data = (E[]) new Object[10];
    In da butt.
    "Do not worry if others do not understand you. Instead worry if you do not understand others." - Confucius
    THE UNDEFEATED SUPERCITIZEN w:4 t:2 l:1 (DON'T ASK!)
    "God is dead" - Nietzsche. "Nietzsche is dead" - God.

    Comment


    • #3
      E[] list;
      ...
      list = new <E> E[size];

      ?
      "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
      Ben Kenobi: "That means I'm doing something right. "

      Comment


      • #4
        Also, that's not how you would go about creating generic arrays anyway.. ???
        In da butt.
        "Do not worry if others do not understand you. Instead worry if you do not understand others." - Confucius
        THE UNDEFEATED SUPERCITIZEN w:4 t:2 l:1 (DON'T ASK!)
        "God is dead" - Nietzsche. "Nietzsche is dead" - God.

        Comment


        • #5
          That works, though it generates a warning. I imagine it's technically unsafe because the Object[] doesn't guarantee it will only accept E's, but it works.

          edit: xpost*2

          Comment


          • #6
            Originally posted by Pekka
            Haven't played with java either for a while, briefly introduced myself to 1.5 (or 5.0.. whatever)..

            Does this help to work it around? Saw this somewhere..


            E[] data = (E[]) new Object[10];
            Actually I think this is right.

            Java is a quirky sonofa***** and you can't initialize generics in any reasonably sensible way, while C# allows you to.

            That should work.
            "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
            Ben Kenobi: "That means I'm doing something right. "

            Comment


            • #7
              Asher's code ( E[size]) generates an error.

              Comment


              • #8
                Thanks.

                Comment


                • #9
                  yeah it gives you a warning but that's OK.... remember, code without warnings is code without passion!
                  In da butt.
                  "Do not worry if others do not understand you. Instead worry if you do not understand others." - Confucius
                  THE UNDEFEATED SUPERCITIZEN w:4 t:2 l:1 (DON'T ASK!)
                  "God is dead" - Nietzsche. "Nietzsche is dead" - God.

                  Comment


                  • #10

                    Comment


                    • #11
                      FUCK YOU JAVA

                      HashSetNode[] list = new HashSetNode[size];

                      "Cannot create a generic array of HashSet.HashSetNode"

                      edit: it's HashSet less than E greater than dot HashSetNode.
                      Last edited by Kuciwalker; September 20, 2006, 23:06.

                      Comment

                      Working...
                      X