• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How Do "Enhanced For Loops" Work?

 
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?
 
Ranch Hand
Posts: 231
1
Eclipse IDE Opera Java
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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
 
Marshal
Posts: 70318
283
  • 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: 70318
283
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic