Polishing for Syntax

Discussion in 'Java' started by Roedy Green, Jun 29, 2005.

  1. Roedy Green

    Roedy Green Guest

    Can this use of Java 1.5 for be make more terse?

    public class ForTest
    {
    /**
    * test harness
    *
    * @param args not used
    */
    public static void main ( String[] args )
    {
    for ( String s : new String[] { "pony", "horse", "mare"} )
    {
    System.out.println( s );
    } // end for
    } // end main
    } // end ForTest

    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
    Roedy Green, Jun 29, 2005
    #1
    1. Advertising

  2. Roedy Green

    Dale King Guest

    Roedy Green wrote:
    > Can this use of Java 1.5 for be make more terse?
    >
    > public class ForTest
    > {
    > /**
    > * test harness
    > *
    > * @param args not used
    > */
    > public static void main ( String[] args )
    > {
    > for ( String s : new String[] { "pony", "horse", "mare"} )
    > {
    > System.out.println( s );
    > } // end for
    > } // end main
    > } // end ForTest
    >


    Depends on your definition of terseness, but refactoring that code into
    a method will let you get rid of the array initializer syntax. You can
    be the judge if that is an improvement in your situation:

    public class ForTest
    {
    public static void print( String... strings )
    {
    for( String s : strings )
    {
    System.out.println( s );
    }
    }
    public static void main( String[] args )
    {
    print( "pony", "horse", "mare" );
    }
    }





    --
    Dale King
    Dale King, Jun 29, 2005
    #2
    1. Advertising

  3. Roedy Green

    Wibble Guest

    Roedy Green wrote:
    > Can this use of Java 1.5 for be make more terse?
    >
    > public class ForTest
    > {
    > /**
    > * test harness
    > *
    > * @param args not used
    > */
    > public static void main ( String[] args )
    > {
    > for ( String s : new String[] { "pony", "horse", "mare"} )
    > {
    > System.out.println( s );
    > } // end for
    > } // end main
    > } // end ForTest
    >

    terse:

    public class ForTest { public static void main(String[] a) {
    System.out.println("pony,horse,mare"); } }
    Wibble, Jun 30, 2005
    #3
  4. Roedy Green

    Roedy Green Guest

    On Wed, 29 Jun 2005 20:20:49 -0400, Wibble <>
    wrote or quoted :

    >> public class ForTest
    >> {
    >> /**
    >> * test harness
    >> *
    >> * @param args not used
    >> */
    >> public static void main ( String[] args )
    >> {
    >> for ( String s : new String[] { "pony", "horse", "mare"} )
    >> {
    >> System.out.println( s );
    >> } // end for
    >> } // end main
    >> } // end ForTest
    >>

    >terse:
    >
    >public class ForTest { public static void main(String[] a) {
    >System.out.println("pony,horse,mare"); } }

    It does not produce the same result. You left out the \ns .

    What I was up to was code for iterating over an ad hoc collection of
    enums, or strings.

    In PL/I you could say something vaguely similar to :

    for ( String s = "pony", "horse","mare" )

    I was hoping there was something in the new 1.5 syntax similar.


    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
    Roedy Green, Jun 30, 2005
    #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. LittleAlex
    Replies:
    0
    Views:
    428
    LittleAlex
    Sep 15, 2008
  2. gabriele renzi
    Replies:
    2
    Views:
    202
    gabriele renzi
    Dec 31, 2005
  3. Ken Bloom
    Replies:
    3
    Views:
    206
  4. Good Night Moon
    Replies:
    9
    Views:
    280
    Rick DeNatale
    Jul 25, 2007
  5. Eric Wong
    Replies:
    0
    Views:
    85
    Eric Wong
    Mar 1, 2010
Loading...

Share This Page