• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Why XX and not X??????

 
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ] ;
}
}
}
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice the "=" in x<=b.length, this is where the ArrayIndexOutOfBoundException come.

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 ] ;
}
}
}
[ November 01, 2003: Message edited by: chi Lin ]
 
Author
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The standard idiom for looping through an array is as follows.

Do you see the difference? In this case, the length of the array is four, but the highest index value is three.
 
Carol Murphy
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doh!!!
What the heck was I thinking?
Oh, wait, I wasn't thinking!
Thanks for pointing that out to me!!!
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic