This week's book giveaway is in the Java 8 forum. We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line! See this thread for details.

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

Joined: Jun 23, 2007
Posts: 5

posted

0

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

Joined: Jun 23, 2007
Posts: 5

posted

0

I think I get it now, the resulting array is used in the for-each loop, not just the initial array.

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 ]