R
Roedy Green
// for each loop gotchas
// In Java 1,5+ you can write a for each loop
// over an array or iterable collection
String[] mystrings = new String[ 50 ];
for ( String s : mystrings )
{
System.out.println ( s );
}
// This is shorthand for:
for ( int i=0; i<mystrings.length; i++ )
{
System.out.println ( mystrings );
}
// However the following code won't compile
// because String is not iterable
String mystring = "happy birthday";
for ( char c : mystring )
{
System.out.println ( c );
}
// you have to write it out longhand
for ( int i=0; i<mystring.length; i++ )
{
System.out.println ( mystring.charAt( i ) );
}
// Further the following code will not compile
string[] mystrings = new String[ 50 ];
for ( int i : mystrings)
{
System.out.println ( mystrings );
}
// you have to write it out longhand
for ( int i=0; i<mystrings.length; i++ )
{
System.out.println ( mystrings );
}
--
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
// In Java 1,5+ you can write a for each loop
// over an array or iterable collection
String[] mystrings = new String[ 50 ];
for ( String s : mystrings )
{
System.out.println ( s );
}
// This is shorthand for:
for ( int i=0; i<mystrings.length; i++ )
{
System.out.println ( mystrings );
}
// However the following code won't compile
// because String is not iterable
String mystring = "happy birthday";
for ( char c : mystring )
{
System.out.println ( c );
}
// you have to write it out longhand
for ( int i=0; i<mystring.length; i++ )
{
System.out.println ( mystring.charAt( i ) );
}
// Further the following code will not compile
string[] mystrings = new String[ 50 ];
for ( int i : mystrings)
{
System.out.println ( mystrings );
}
// you have to write it out longhand
for ( int i=0; i<mystrings.length; i++ )
{
System.out.println ( mystrings );
}
--
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