Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt In foreach Loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt In foreach Loop" Watch "Doubt In foreach Loop" New topic
Author

Doubt In foreach Loop

srinibash udayasingh
Greenhorn

Joined: Jul 09, 2007
Posts: 22
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
Abdullah Mamun
Ranch Hand

Joined: Mar 19, 2007
Posts: 99


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 ]

MooN
srinibash udayasingh
Greenhorn

Joined: Jul 09, 2007
Posts: 22
Thank You very Much for your good explanation.Now my doubt cleared
Mohammed shareef
Greenhorn

Joined: Aug 08, 2007
Posts: 10
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

Joined: Aug 08, 2007
Posts: 10
Excellent Al Mamun ,but you have not explain why control is going to i+1 position,then coming back to i position.
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
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

Joined: Mar 19, 2007
Posts: 99

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.
Rishiraj Bayerd
Greenhorn

Joined: Aug 06, 2007
Posts: 9
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
Amitji Sharma
Greenhorn

Joined: Dec 26, 2006
Posts: 16
Thanks Al Mamun
for such a Good Explanation.


Amit Sharma<br />There is only one proof of ability that's Result.
Neha Bhattad
Ranch Hand

Joined: Aug 20, 2007
Posts: 30
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

Joined: Aug 06, 2007
Posts: 9
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

Joined: Mar 19, 2007
Posts: 99

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt In foreach Loop
 
Similar Threads
using set and get methods
Can you please explain how output is 0 0 3 0
Arrays
enhanced for loop problem -- from a mock
Array...