• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

output?

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


why does this print me
1
0
3
0
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using this loop, the first time through you pull the first array element and assign it to i, therefore i = 1. It will print 1, then assign array element arr[1] = 0. Going through the 2nd time, you set i = 0, since that is now the 2nd element in the array. Then you set arr[0] = 0. Then you pull the 3rd element, 3, and assign it to i. Then you set arr[3] = 0. Then you pull the 4th element, 0, and print it. Then you set arr[0] = 0, and the loop finishes.

The important thing to remember here is arrays are zero-based.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joe.......How you doin?
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankur kothari wrote:

why does this print me
1
0
3
0


The iteration is:

loop will run the length of arr;(i.e. 4)
arr{0=1, 1=2, 2=3, 3=4}

first iteration whereas: i=arr[0]=1;
System.out.println(i); // i=1;
arr[i]=0; //arr[1]=0

second iteration whereas: i=arr[1]=0; as we altered arr[1] position in first iteration;
System.out.println(i); // i=0;
arr[i]=0; //arr[0]=0

third iteration whereas: i=arr[2]=3;
System.out.println(i); // i=3;
arr[i]=0; //arr[3]=0


fourth iteration whereas: i=arr[3]=0; as we alter arr[3]=0 position in third iteration;
System.out.println(i); // i=0;
arr[3]=0; //arr[3]=0

Loop exits;

Hope its clear to you now.


Minhaj
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankur, when you post a mock exam question, please QuoteYourSources of the question.

Also please UseAMeaningfulSubjectLine for your topics. Instead of writing "output?" if you write something like "confusion in enhanced for loop output", it makes more sense...
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic