Polishing for Syntax

R

Roedy Green

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
 
D

Dale King

Roedy said:
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" );
}
}
 
W

Wibble

Roedy said:
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"); } }
 
R

Roedy Green

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top