wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enhanced For Loop Problem 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 "Enhanced For Loop Problem" Watch "Enhanced For Loop Problem" New topic
Author

Enhanced For Loop Problem

Charandeep Singh
Ranch Hand

Joined: May 06, 2007
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

Joined: Feb 23, 2007
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
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Charandeep Singh
Ranch Hand

Joined: May 06, 2007
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
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

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

Joined: May 06, 2007
Posts: 57
Hi Henry
Thanks for the explanation
I got your point
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

Joined: Feb 20, 2007
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

Joined: May 06, 2007
Posts: 57
Thanks a lot megha
Got it
Logical mistake from my end
thanks a lot for clarifying it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enhanced For Loop Problem
 
Similar Threads
2 dim array
Arrays
queries
John Meyer's SCJP 5 mock exam doubt
enhanced for loop problem -- from a mock