aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enhanced For Loop For Array Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Enhanced For Loop For Array" Watch "Enhanced For Loop For Array" New topic
Author

Enhanced For Loop For Array

Sankar Raj
Greenhorn

Joined: May 29, 2008
Posts: 2

Kindly explain the way the enhanced for loop iterates and the way it varies from the conventional for loop with the below example ?

Source : J.I Inquisition John Meyers SCJP5 Mock Exam

Question :

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

Answer :

Prints 0030

As with the conventional for loop i thought it would print 0000.

To understand the way it works, i tried out as below

for ( int i : arr )
{
System.out.println("New For Index "+ i);
arr[i] = 0;
System.out.println("New For "+ i + " Value " + arr[i]);
System.out.println(" ********* ");
}

for ( int i : arr )
{
System.out.println("New For Output "+i);
}

and got below as output,

New For Index 1
New For 1 Value 0
*********
New For Index 0
New For 0 Value 0
*********
New For Index 3
New For 3 Value 0
*********
New For Index 0
New For 0 Value 0
*********
New For Output 0
New For Output 0
New For Output 3
New For Output 0


Help me out in getting the result
Ivan Ivanic
Ranch Hand

Joined: Oct 31, 2007
Posts: 100
This is quite easy to understand:
For-each loop has internal counter that moves pointer to next element in each iteration.
So:
1st iteration
here is how array looks:

So code

assigns value of 1 to i because pointer points to that value in arr.
That said code looks like this:

and changes arr to look like this:


2nd iteration:
Pointer moves to the next value:

So code

assigns value of 0 to i because pointer points to that value in arr.
That said code looks like this:

and changes arr to look like this:

3rd iteration:
Pointer moves to the next value:

So code

assigns value of 3 to i because pointer points to that value in arr.
That said code looks like this:

and changes arr to look like this:

4th iteration:
Pointer moves to the next value:

So code

assigns value of 0 to i because pointer points to that value in arr.
That said code looks like this:

arr[0] is changed but to the same value and looks like this:


I hope this helps


<a href="http://faq.javaranch.com/java/UseRealWords" target="_blank" rel="nofollow">Use Real Words</a> <a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">!!!Use Code Tags!!!</a> <a href="http://faq.javaranch.com/java/SayThanks" target="_blank" rel="nofollow">Say Thanks</a><br />scjp6
Sankar Raj
Greenhorn

Joined: May 29, 2008
Posts: 2
Thank You. I was confused & tensed as my exam date is nearing.. thanks for helping me out..
Ivan Ivanic
Ranch Hand

Joined: Oct 31, 2007
Posts: 100
On The Exam you will have paper and pen or whiteboard and marker. Always use them to visualize things that are hard for you to follow: execution of loops, creation/garbage collection of objects, inheritance... Draw, write, sketch use lines, rectangles, dots - anything to keep your mind working.
Take your time to actually think, that way you will not get stuck in a place.
All the best wishes for your exam,
Ivan.
Chaitanya Lele
Greenhorn

Joined: Dec 23, 2008
Posts: 19
Consider the following code



The output produced is 0 0 0 0

But according to Ivan's explanation above the output should be 0 0 1 0

Really confused here...


Finally did it!! SCJP 6 certified!!!
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331

Wow. This is a tricky question.

The thing is that the elements of the array are of course { 0, 0, 1, 0}

But when you're printing you are not printing i but arr[i]... See the difference?


Vishwa


Blog
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Enhanced For Loop For Array
 
Similar Threads
Why will I get this output?
enhanced for loop problem -- from a mock
need help on precedence of operators
queries
Enhanced For Loop Problem