Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Polishing for Syntax

Thread view: 
Roedy Green - 29 Jun 2005 07:55 GMT
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

Signature

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

Dale King - 29 Jun 2005 08:27 GMT
> Can this use of Java 1.5 for be make more terse?
>
[quoted text clipped - 13 lines]
>       } // 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" );
    }
}

Signature

 Dale King

Wibble - 30 Jun 2005 01:20 GMT
> Can this use of Java 1.5 for be make more terse?
>
[quoted text clipped - 13 lines]
>       } // end main
>    } // end ForTest

terse:

public class ForTest { public static void main(String[] a) {
System.out.println("pony,horse,mare"); } }
Roedy Green - 30 Jun 2005 07:32 GMT
>> public class ForTest
>>    {
[quoted text clipped - 16 lines]
>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.

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.