• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

for each

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain why the output to the following code is: 0 0 3 0



I would think there would be an ArrayOutOfBounds exception in the first for loop because it tries to set arr[4] = 0.

Thanks.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jason,
In the first iteration i=1 which sets arr[1]=0 .The resulting array is {1,0,3,4}
In the second iteration i=0 which sets arr[0]=0. The resulting array is {0,0,3,4}
In the third iteration i=3 which sets arr[3]=0. The resulting array is {0,0,3,0}
In the last iteration i=0 which sets arr[0]=0. The resulting array is {0,0,3,0}
 
jason roberts
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sravani,

In the first iteration i=1 which sets arr[1]=0 .The resulting array is {1,0,3,4}
In the second iteration i=0 which sets arr[0]=0. The resulting array is {0,0,3,4}
In the third iteration i=3 which sets arr[3]=0. The resulting array is {0,0,3,0}
In the last iteration i=0 which sets arr[0]=0. The resulting array is {0,0,3,0}



I guess I don't understand why it isn't:

In the first iteration i=1 which sets arr[1]=0 .The resulting array is {1,0,3,4}
In the second iteration i=2 which sets arr[2]=0. The resulting array is {1,0,0,4}
In the third iteration i=3 which sets arr[3]=0. The resulting array is {1,0,0,0}
In the last iteration i=4 which attempts to set arr[4]=0, and throws an exception.

I thought that since the array is {1,2,3,4}, the index values on each iteration would be 1,2,3 and 4.

Please help.

Thanks.
 
jason roberts
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I get it now, the resulting array is used in the for-each loop, not just the initial array.

Thanks for your help.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anybody explain why in iteration 2 i becomes 0 and in 4th iteration too also? how the value of i is changing. ?
[ June 27, 2007: Message edited by: nitin pokhriyal ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can anybody explain why in iteration 2 i becomes 0 and in 4th iteration too also?



in the first iteration i initialized with arr[0] means i=1 so in the loop a[i=1]=0 which actually second element in the array.

same explanation for the third iteration.

I hope this gives you clear idea.
 
nitin pokhriyal
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks buddy
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jason roberts:
the resulting array is used in the for-each loop, not just the initial array.



There is no initial or resulting array. You have only one array. In your example the for loop is translated to something like this:
 
reply
    Bookmark Topic Watch Topic
  • New Topic