posted 15 years ago
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