Okay you gurus of
java, I can't figure out what the difference is between these two programs. One compiles and runs just fine, the other doesn't. The difference is in the flip() method. One explicitly goes through the array, the other tries to use a for loop to do the same, but it doesn't work. Why won't the for loop work?
class XX
{
public static void main(
String[] args )
{
boolean[] b = new boolean[ 4 ] ;
b[ 0 ] = false ;
b[ 1 ] = true ;
b[ 2 ] = false ;
b[ 3 ] = true ;
System.out.println( b[ 0 ] ) ;
System.out.println( b[ 1 ] ) ;
System.out.println( b[ 2 ] ) ;
System.out.println( b[ 3 ] ) ;
flip( b ) ;
System.out.println( b[ 0 ] ) ;
System.out.println( b[ 1 ] ) ;
System.out.println( b[ 2 ] ) ;
System.out.println( b[ 3 ] ) ;
}
static void flip( boolean[] b )
{
System.out.print("b is "+ b.length +" items long. \n" );
b[ 0 ] = !b[ 0 ] ;
b[ 1 ] = !b[ 1 ] ;
b[ 2 ] = !b[ 2 ] ;
b[ 3 ] = !b[ 3 ] ;
}
}
class X
{
public static void main( String[] args )
{
boolean[] b = new boolean[ 4 ] ;
b[ 0 ] = false ;
b[ 1 ] = true ;
b[ 2 ] = false ;
b[ 3 ] = true ;
System.out.println( b[ 0 ] ) ;
System.out.println( b[ 1 ] ) ;
System.out.println( b[ 2 ] ) ;
System.out.println( b[ 3 ] ) ;
flip( b ) ;
System.out.println( b[ 0 ] ) ;
System.out.println( b[ 1 ] ) ;
System.out.println( b[ 2 ] ) ;
System.out.println( b[ 3 ] ) ;
}
static void flip( boolean[] b )
{
System.out.print("b is "+ b.length +" items long. \n" );
for( int x = 0; x<=b.length; x++)
{
b[ x ] = !b[ x ] ;
}
}
}