• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enhanced Loop

 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know the working of Enhanced loop in a simple method.
I did understand it once but then i forgot
Does any one has a simple way so i can understand it and that it stays in my brain!?!
There were various posts on it, but i can't find it.
If some one has the link please pass it on.

This was a small code i wrote.
I can't understand the second part of the result.
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enhanced for loop does one and only one thing, looping through all elements of an given array or Iterable object.

It is like, saying "for each object in the given array or iterable object, do something". While looping through the array or Iterable object, assigns the currently positioning element into given variable so that you access the element in the loop body.

Back to your code:

In the first loop, you are looping through the array and printing the array values.

In the second loop, you are looping through the array, using the values as index to select the array values.

First iteration, var i would be "9" and so array value at position "9" is "0"; That's what you are printing.
 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh Thanks!

Looks like i was thinking more then i should!!

There was a post about some mock question which used an Enhaced Loop and where the result was 0030 or something of that sort.
Ther was a long discussion on it.

Actually there were 2 posts on it.

Doesn any one has a link to it?
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey I don't know where that post is but the code was this

 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic