# How does enhanced for loop behave?

Mujahid Mehdi

Greenhorn

Posts: 2

posted 8 years ago

I am getting the output as 0 0 3 0 5 0 7 0 for the following program:-

class VarArgs{

public static void main(String... args){

int[] arr = {1,2,3,4,5,6,7,8};

for (int i : arr){

arr[i] = 0;

}

for(int i: arr){

System.out.println(i);

}

}

}

Could any one explain how enhanced for loop behaves?

class VarArgs{

public static void main(String... args){

int[] arr = {1,2,3,4,5,6,7,8};

for (int i : arr){

arr[i] = 0;

}

for(int i: arr){

System.out.println(i);

}

}

}

Could any one explain how enhanced for loop behaves?

posted 8 years ago

Enhanced for loops work pretty much as you would expect, following the logic "for each whatever in a list of whatevers do something".

However, this:

is changing your list of "whatevers" while you iterate through it, so that will muddy the waters a bit.

Think about it:

The first iteration, i == 1, so you change the The second iteration, i == 0 (because you changed this value in the first iteration) so you change the The third iteration, i == 3, so you change the The forth iteration, i == 0 (because you changed this value in the third iteration) so you change the

And so on. Stick in some debug lines to watch what the values are doing if you don't quite follow the above.

[ April 09, 2008: Message edited by: Paul Sturrock ]

However, this:

is changing your list of "whatevers" while you iterate through it, so that will muddy the waters a bit.

Think about it:

*second*element in your arr to be 0.

*first*element of arr to be 0.

*forth*element in arr to be 0.

*first*element of arr to be 0.

And so on. Stick in some debug lines to watch what the values are doing if you don't quite follow the above.

[ April 09, 2008: Message edited by: Paul Sturrock ]

Balasubramani Dharmalingam

Ranch Hand

Posts: 116

posted 8 years ago

What is the output that you are expecting from the above code ?

It is working fine.

So for

i = 1

a[1] = 0

array = {1,0,3,4,5,6,7,8}

i=0

a[0] = 0

array = {0,0,3,4,5,6,7,8}

i=3

a[3]=0

array = {0,0,3,0,5,6,7,8}

...

..

...

After completing the eight iteration value in the array will be

{0,0,3,0,5,0,7,0}

It is working fine.

So for

**First Iteration**i = 1

a[1] = 0

array = {1,0,3,4,5,6,7,8}

**Second Iteration**i=0

a[0] = 0

array = {0,0,3,4,5,6,7,8}

**Third Iteration**i=3

a[3]=0

array = {0,0,3,0,5,6,7,8}

...

..

...

After completing the eight iteration value in the array will be

{0,0,3,0,5,0,7,0}

Balasubramani SD,<br />SCJP 1.4,SCWCD 1.4,SCJP 5.0<br /><a href="http://sd.balasubramani.googlepages.com" target="_blank" rel="nofollow">www.sd.balasubramani.googlepages.com</a>

Joanne Neal

Rancher

Posts: 3742

16

posted 8 years ago

At start your array contains {1,2,3,4,5,6,7,8};

First time thru the first loop i will be 1, so it sets the second element of your array (arrays are zero based) to 0, so your array contains {1,0,3,4,5,6,7,8}

Second time thru the first loop i will be 0, so it sets the first element of your array to 0, so your array contains {0,0,3,4,5,6,7,8}

Third time thru the first loop i will be 3, so it sets the fourth element of your array to 0, so your array contains {0,0,3,0,5,6,7,8}

Fourth time thru the first loop i will be 0, so it sets the first element of your array to 0, so your array contains {0,0,3,0,5,6,7,8}

Fifth time thru the first loop i will be 5, so it sets the sixth element of your array to 0, so your array contains {0,0,3,0,5,0,7,8}

Sixth time thru the first loop i will be 0, so it sets the first element of your array to 0, so your array contains {0,0,3,0,5,0,7,8}

Seventh time thru the first loop i will be 7, so it sets the eighth element of your array to 0, so your array contains {0,0,3,0,5,0,7,0}

Eigth time thru the first loop i will be 0, so it sets the first element of your array to 0, so your array contains {0,0,3,0,5,0,7,0}

First time thru the first loop i will be 1, so it sets the second element of your array (arrays are zero based) to 0, so your array contains {1,0,3,4,5,6,7,8}

Second time thru the first loop i will be 0, so it sets the first element of your array to 0, so your array contains {0,0,3,4,5,6,7,8}

Third time thru the first loop i will be 3, so it sets the fourth element of your array to 0, so your array contains {0,0,3,0,5,6,7,8}

Fourth time thru the first loop i will be 0, so it sets the first element of your array to 0, so your array contains {0,0,3,0,5,6,7,8}

Fifth time thru the first loop i will be 5, so it sets the sixth element of your array to 0, so your array contains {0,0,3,0,5,0,7,8}

Sixth time thru the first loop i will be 0, so it sets the first element of your array to 0, so your array contains {0,0,3,0,5,0,7,8}

Seventh time thru the first loop i will be 7, so it sets the eighth element of your array to 0, so your array contains {0,0,3,0,5,0,7,0}

Eigth time thru the first loop i will be 0, so it sets the first element of your array to 0, so your array contains {0,0,3,0,5,0,7,0}

Joanne