5 Replies Latest reply on Jan 28, 2009 6:19 PM by 796440

    Lookup in array literal?

    807588
      Hello,

      Does Java support lookups from array literals? Something like this:
      System.out.println(["one", "two", "three"][1]); // outputs "two"
      Yeah I know it's bad practice, I'm just messing around with some obfuscated code.

      Thanks,

      Z.
        • 1. Re: Lookup in array literal?
          796440
          Try it.

          And if you find a context in which
          ["one", "two", "three"]
          is legal, with or without the "lookup", let me know.
          • 2. Re: Lookup in array literal?
            dcminter
            System.out.println(new String[] {"One", "Two", "Three"}[1]);
            @jverd - I think maybe you misinterpreted what he was after. Since he doesn't know the syntax for what he's doing it's not surprising that he got a bit mixed up there...

            Edited by: dcminter on 28-Jan-2009 17:43
            • 3. Re: Lookup in array literal?
              807588
              I already tried, here are the errors I receive:
              test.java:6: illegal start of expression
                          System.out.println(["one", "two", "tree"][1]);
                                             ^
              test.java:6: ';' expected
                          System.out.println(["one", "two", "tree"][1]);
                                              ^
              test.java:6: illegal start of expression
                          System.out.println(["one", "two", "tree"][1]);
                                                   ^
              test.java:6: ';' expected
                          System.out.println(["one", "two", "tree"][1]);
                                                    ^
              test.java:6: illegal start of expression
                          System.out.println(["one", "two", "tree"][1]);
                                                          ^
              test.java:6: ';' expected
                          System.out.println(["one", "two", "tree"][1]);
                                                           ^
              test.java:6: illegal start of expression
                          System.out.println(["one", "two", "tree"][1]);
                                                                  ^
              test.java:6: ';' expected
                          System.out.println(["one", "two", "tree"][1]);
                                                                   ^
              test.java:6: not a statement
                          System.out.println(["one", "two", "tree"][1]);
                                                                    ^
              test.java:6: ';' expected
                          System.out.println(["one", "two", "tree"][1]);
                                                                     ^
              10 errors
              I also tried {....}[0] as well as String[...][0] and got similar errors.
              • 4. Re: Lookup in array literal?
                807588
                Thank you dcminter.
                • 5. Re: Lookup in array literal?
                  796440
                  dcminter wrote:
                  System.out.println(new String[] {"One", "Two", "Three"}[1]);
                  @jverd - I think maybe you misinterpreted what he was after. Since he doesn't know the syntax for what he's doing it's not surprising that he got a bit mixed up there...

                  Edited by: dcminter on 28-Jan-2009 17:43
                  Right.

                  I was hoping to get him to break it down into two parts: 1) A valid "array literal" expression, or whatever that'd be called, and 2) Getting an element from an array expression--see if what works with, e.g., an array variable also works with the valid "array literal".