Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes A simple syntax question, pls help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A simple syntax question, pls help" Watch "A simple syntax question, pls help" New topic
Author

A simple syntax question, pls help

Jon Lee
Ranch Hand

Joined: Mar 04, 2005
Posts: 134
The following code is taken from "Core Java 7ED". There is one line of code I can not understand. That line No. is 90. What does it mean??? I never read a "for" loop code like this. Pls give some hints, Thnx a lot!



[ August 13, 2005: Message edited by: Jon Lee ]
[ August 13, 2005: Message edited by: Jon Lee ]

SCJP 5.0 - 98% (2007)<br />SCWCD 1.4 - 97% (2007)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
It's the new for loop construct that appeared in Java 5.0. You can read it as "for each Ball b in the collection (or array) balls, perform the following (whatever is in the {})". It is known as the "foreach" construct.
You can use break or continue as you would in a normal loop.
[ August 13, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
this is the enhanced for loop as said. It means go through every element of balls ArrayList as you go through the list one at a time starting from [0]and put the value in b each time you do so.


I'm a Hood Ornament
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Are you reading Volume 1-Fundamentals or Volume 2-Advanced Topics? Volume 1 will explain this and other new features that come with Java 5.0, so it might be worth your time buying the new edition of Volume 1 even if you already own an older edition.

Layne


Java API Documentation
The Java Tutorial
Jon Lee
Ranch Hand

Joined: Mar 04, 2005
Posts: 134
Oh, I see. I dont read volume one yet. Maybe I should go through volume one qucikly to see the new features in Java 5.0.

Thnx for your replies, guys.

[Please do not offer to share copyrighted materials through JavaRanch. In fact, don't offer to share them at all. The people who produce such things deserve to be paid properly for their work -- mfe]

Cheers
[ August 16, 2005: Message edited by: Michael Ernest ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
That's a very generous offer, but if the authors or publishers see it, you may be heading for some trouble since they own the copyrights and probably frown on people giving it away without their permission. In fact, the authors were here at the Ranch fairly recently for a book promotion...

Layne
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Layne Lund:
That's a very generous offer, but if the authors or publishers see it, you may be heading for some trouble since they own the copyrights and probably frown on people giving it away without their permission. In fact, the authors were here at the Ranch fairly recently for a book promotion...

Layne


Thank you for mentioning that Layne. Jon please take notice.
Jon Lee
Ranch Hand

Joined: Mar 04, 2005
Posts: 134
Guys, thnx for your reminder. I will not share the books any more. BTW, the CORE Java series are great books for beginners. It is worth your money.
 
jQuery in Action, 2nd edition
 
subject: A simple syntax question, pls help
 
Similar Threads
exception java.lang.InterruptedException is never thrown in body of corresponding try statement
C++ish Address &
New to Java. Need some proformance suggestions
Help needed with the Code
dx = -dx; dy = -dy