Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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!!!
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic