• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enhanced For Loop For Array

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sankar Raj
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You. I was confused & tensed as my exam date is nearing.. thanks for helping me out..
 
Ivan Ivanic
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic