This week's book giveaways are in the Refactoring and Agile forums.We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!

# Enhanced For Loop Problem

Charandeep Singh
Ranch Hand
Posts: 57
We have a prog
class ArrTest
{
public static void main(String[] args)
{
int []arr = {1,2,3,4};

for(int i=0;i<arr.length;i++){// line 1
System.out.println(i);
arr[i]=0;
}

for(int j : arr)

System.out.println(j);
}
}

The output is
0
1
2
3
0
0
0
0
Where as if i use enhanced for loop in place of line 1
the out put is
1
0
3
0
0
0
3
0

I always thought they both worked same way
can any body explain the different usage between these two for loops

Thanks
Charandeep

anil kumar
Ranch Hand
Posts: 447
---------------------------------------------------------------------
for(int i=0;i<arr.length;i++){// line 1
System.out.println(i);//line 2
arr[i]=0;//line 3
--------------------------------------------------------------------

At line 2 you are printing the value of varibale i

and at line 3 you are assigning the 0 to every element in the array

if you replace the line 1 like this
for(int j:arr)
System.out.println(j);

you will get

1
2
3
4

But if you place the enhanced for loop after line 3
you will get alll 0's.Because you assigned the array elements to 0.

Thanks
Anil Kumar

Henry Wong
author
Marshal
Posts: 20889
75
What do you mean by this statement?

Where as if i use enhanced for loop in place of line 1
the out put is

Do you mean this?

If you did this, then it translates back to...

Remember, with the enhanced for loop. The loop variable is not the index, it is the element of the array.

Henry

Charandeep Singh
Ranch Hand
Posts: 57
I also always felt that when we use the enhanced for loop
it translates back to orignal for loop only
but

class ArrTest
{
public static void main(String[] args)
{
int []arr = {1,2,3,4};

for(int i: arr){
System.out.println("x" +i);
arr[i]=0;
}

for(int j : arr)

System.out.println("Hello World! x" +j);

}
}

The output is
x 1
x 0
x 3
x 0
Hello World! x 0
Hello World! x 0
Hello World! x 3
Hello World! x 0

But now if you change the program some what like this
class ArrTest
{
public static void main(String[] args)
{
int []arr = {1,2,3,4};

for(int i=0;i<arr.length;i++){
System.out.println("x" +i);
arr[i]=0;
}

for(int j : arr)

System.out.println("Hello World! x" +j);

}
}

The output is
x 0
x 1
x 2
x 3
Hello World! x 0
Hello World! x 0
Hello World! x 0
Hello World! x 0

This is the difference I am talkin about
Can you please explain why and wha is the difference between the two for loops.

Thanks
Charandeep

Henry Wong
author
Marshal
Posts: 20889
75
This is the difference I am talkin about
Can you please explain why and wha is the difference between the two for loops.

Charandeep,

Please go back an re-read my response. Basically, the variable "i" in the enhanced for loop is *not* the index, but the value.

This...

And this...

Are not the same logic...

Henry
[ May 12, 2007: Message edited by: Henry Wong ]

Charandeep Singh
Ranch Hand
Posts: 57
Hi Henry
Thanks for the explanation
My question is
when the enhanced for loop is used on line 1
and after that arr[i]
that means value of i will be 1,2,3,4
so when it reaches 4 it should give ArrayIndexOutOfBoundException
which it doesn't
where as if you remove arr[i]=0;
It does give that
Can you clarify it further

Thanks
Charan

megha joshi
Ranch Hand
Posts: 206
Hi,

If you do the following

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

On first iteration
i = 1;
a[i] = 0 => a[1] = 0 => arr = {1,0,3,4}

On second iteration
i = 0;
a[i] = 0 => a[0] = 0 => arr = {0,0,3,4}

On third iteration
i = 3
a[i] = 0 => a[3] = 0 => arr = {0,0,3,0} //this is the step tht prevents the OutOfBoundsexcetion

On forth iteration
i = 0
a[i] = 0 => a[4] = 0 => arr = {0,0,3,0}

This was tricky...I hope the above helps...

Thanks,
Megha

Charandeep Singh
Ranch Hand
Posts: 57
Thanks a lot megha
Got it
Logical mistake from my end
thanks a lot for clarifying it

 It is sorta covered in the JavaRanch Style Guide.