• Post Reply Bookmark Topic Watch Topic
  • New Topic

How Do "Enhanced For Loops" Work?  RSS feed

 
Kevin Corina
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, just wondering if anyone could explain how to use an Enhanced For Loop. The book I've been using was kind of vague on the subject, (Head First Java, 2nd Edition) and I kind of want to understand it before I move on. I already did some research online, but was confused more so.


So, keeping in mind I am EXTREMELY new to java, how do you use an Enhanced For Loop, and why would I need to use one?
 
Rajdeep Biswas
Ranch Hand
Posts: 231
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome number 2 to Kevin. Welcome to the Ranch.
Lets try not to make this discussion vague. Tell the world here what's your current understanding and issues/doubts.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Here is Oracle's page which explains it: The For-Each Loop
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again.
You have to imagine that the loop creates a temporary variable which is a copy of the next element in the List. You can use that temporary varaible, and change it, but that does not affect the original List/array/whatever. The Oracle link which Jesper quoted does say it doesn’t cover all possibilities, but they preferred to go for simple syntax.
 
Kevin Corina
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome again.
You have to imagine that the loop creates a temporary variable which is a copy of the next element in the List. You can use that temporary varaible, and change it, but that does not affect the original List/array/whatever. The Oracle link which Jesper quoted does say it doesn’t cover all possibilities, but they preferred to go for simple syntax.


OOOOHHHH... ok... that makes a lot more sense, thanks for the help!
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!