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

# Doubt In foreach Loop

Greenhorn
Posts: 22
• Number of slices to send:
Optional 'thank-you' note:
Hello Ranchers
can any one explain the following code snipets

int []arr = {1,2,3,4};
for ( int i : arr )
{
arr[i] = 0;
}
for ( int i : arr )
{
System.out.println(i);
}

out put here is :0030

Ranch Hand
Posts: 99
• Number of slices to send:
Optional 'thank-you' note:

After executing this code the arr[] will contain {0,0,3,0}
How??? -- lets see

executing for loop and retrieving the first element which is 1 or i = 1
set arr[1] = 0;
now arr = {1,0,3,4} // Look the second element became 0 now

looping again and retrieving second element which is 0 or i = 0
set arr[0] = 0;
now arr = {0,0,3,4}

looping again and retrieving third element which is 3 or i = 3
set arr[3] = 0; // Look the fourth element became 0 now
now arr = {0,0,3,0}

looping again and retrieving fourth and last element which is 0 or i = 0
set arr[0] = 0;
now arr = {0,0,3,0}

So the final array is {0,0,3,0} and thats why you are getting this output.

Hope this helps
[ August 22, 2007: Message edited by: Al Mamun ]

srinibash udayasingh
Greenhorn
Posts: 22
• Number of slices to send:
Optional 'thank-you' note:
Thank You very Much for your good explanation.Now my doubt cleared

Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
Instead Of Anonymous array use normal array declaration &initialization,u will get expected output.There is some mystery with enhance for with anonymous array & if we increase length of Anonymous array to 5 it is giving ArrayIndexOutOfBound Exception .Let's wait &see who willl solve this mystery.

Mohammed shareef
Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
Excellent Al Mamun ,but you have not explain why control is going to i+1 position,then coming back to i position.

Ranch Hand
Posts: 377
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Mohammed shareef:
if we increase length of Anonymous array to 5 it is giving ArrayIndexOutOfBound Exception .Let's wait &see who willl solve this mystery.

There is no mystery about the exception. Just follow the explanation from Al Mamun and add the following:

looping again and retrieving fifth element which is ... or i = ...
set arr[...] = 0;

I think it should be clear why the ArrayIndexOutOfBoundException occurs.

Abdullah Mamun
Ranch Hand
Posts: 99
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Mohammed shareef:

Excellent Al Mamun ,but you have not explain why control is going to i+1 position,then coming back to i position.

Actually, there is no pattern or any other thing like going to i+1 or coming back to i position in this example. The for loop is traversing through the array and the value of each index of the array is determining the value of i.

Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
Hi Al Mamun,
Excellent explanation. Simply superb� if we take int [] arr={10,20,30,40} instead of int [] arr={1,2,3,4} we got ArrayOutofBound exception , because at for(i:arr), the value of is 10 i.e value of first element(value 0th place) and that becomes use as index of arr at line arr[i]=0;

with regards
Rishi

Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
Thanks Al Mamun
for such a Good Explanation.

Ranch Hand
Posts: 30
• Number of slices to send:
Optional 'thank-you' note:
Thanks Al Mamun.

Instead Of Anonymous array use normal array declaration &initialization,u will get expected output.

My question is: Why would the answer be any diffent if it wasnt an Anonymous array? I tried and im getting the same response 0030.

Rishiraj Bayerd
Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
Hi Neha,
i think it does not depends on array type(i.e. Anonymous,Simple). All mistry hide at first foreach loop. I try to draw the picture.

Original code:
for(int i: arr1) arr1[i] = 0;

int i:arr1 means the value of the i is the first element of arr1(0th place of arr1) i.e. the value of the i is 1.

For fisrt time... Then arr1[i]=0 becomes arr1[1]=0 .
So,(Original array) {1,2,3,4}=>{1,0,3,4}

Again for second time...
The value of the i is the second element of arr1(1st place of arr1) i.e. the value of the i is 0. Then arr1[i]=0 becomes arr1[0]=0
So,(Original array) {1,0,3,4}=>{0,0,3,4}

Again for Third time...
The value of the i is the third element of arr1(2nd place of arr1) i.e. the value of the i is 3. Then arr1[i]=0 becomes arr1[3]=0
So,(Original array) {0,0,3,4}=>{0,0,3,0}

Again for last time...(length of arr1 is 4)
The value of the i is the fourth element of arr1(3rd place of arr1) i.e. the value of the i is 0. Then arr1[i]=0 becomes arr1[0]=0 So,(Original array) {0,0,3,0}=>{0,0,3,0}

So the final output will be 0030

thanks....

with regards

Rishi

Abdullah Mamun
Ranch Hand
Posts: 99
• Number of slices to send:
Optional 'thank-you' note:
Hi Neha

Originally posted by Mohammed shareef:
...Instead Of Anonymous array use normal array declaration &initialization,u will get expected output....

This statement is not correct here. The output is totally depends on the first for-each loop and the assignment inside the loop. So don't worry

 Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad. a bit of art, as a gift, the permaculture playing cards https://gardener-gift.com