This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Colon : in for{} loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Colon : in for{} loop?" Watch "Colon : in for{} loop?" New topic
Author

Colon : in for{} loop?

Jon Campbell
Greenhorn

Joined: Nov 21, 2006
Posts: 5
Taking a Java test I found a for{} loop which had a : character in it?... like this...

for { : }

I was supposed to fill in some objects, but I have no idea how a : character does in a for loop?

Any comments?
JON
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

This is the "enhanced for loop," which was introduced in Java 1.5. Basically, it's a way of saying "for each element in some Collection (or array)..." This is a shortcut so you don't need to work directly with iterators or indexes.

For example...

Also see Java Nuts and Bolts - The for Statement.
[ December 24, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by marc weber:
This is the "enhanced for loop," which was introduced in Java 1.5. Basically, it's a way of saying "for each element in some Collection (or array)..." This is a shortcut so you don't need to work directly with iterators or indexes.


That's nice to know, somebody besides Walter Bright has built in the first syntax the beginner always tries to implement first.

Maybe we ought to watch Krakatoa for rumblings. :roll:
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11524
    
100

Hi Jon,

The only thing I would add to Marc's comments is that there is a benefit to the new for loop which Marc's code uses, but which might not be obvious: you automatically get a variable which is the right type. And even if you have to cast it within the for loop, it often is easier to read than if you had to do it within the body of the old style of loop.

What I mean by that is that in Marc's code, the for loop is declared as:Within that loop, s is already known to be a String, and can be treated as such.

Now lets look at some (deliberately) ugly code to show how this can be beneficial:In line 5 I deliberately decided not to use generics. (This is noticeable when you try and compile this code - it will complain about unsafe operations.) If I had used generics, then a lot of what I am trying to demonstrate just couldn't be demonstrated - the big aim of Java 5 was making life easier for the programmer after all. But this example is still valid - you might be getting data from a legacy source which does not use generics, so you will have to work with it.

In lines 11 through 14 I show one way of dealing with this. I know that I am getting a Pet object from my List, so in line 12 I cast the base Object to a Pet object, which I can then use in line 13.

Lines 16 through 18 do the same thing, however the casting and the use of the cast object are all done inline. (This is just to silence critics who will otherwise tell me that my code is too verbose. )

Lines 20 through 22 show how casting within the new for loop itself can result (IMHO) in more readable code - I end up with a Pet object that I can use in the body of the for loop.

Note: there is a downside to the new for loop - you have lost the automatic position counter that the old style for loop forced you to have. So in line 16 I declared a variable i, which I can then use to know how far through the loop I am (at the point I am processing the "Ferret" instance, i will be set to 2, which I can compare with the size() of the list, to find out that I am (2 + 1) / 4 = 3/4 of the way through the list. Or I could use i for some other purpose). This counter is no longer available in the new form.

Regards, Andrew

[ December 24, 2006: Message edited by: Andrew Monkhouse ]
[ December 24, 2006: Message edited by: Andrew Monkhouse ]

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by jackaroo:

What is a jackaroo ?
  • A limited experience coder.
  • Distant genalogical of the jackalope.
  • An eager kanga-roo.
  • A ranching implement.


  • /*********/
    a benefit to the new for loop which Marc's code uses, but which might not be obvious: you automatically get a variable which is the right type.
    You mean new style for loop, correct ? Not the keyword new....(?)

    [ added 4:20 PM 1/4/2007 by: Nicholas Jordan - it is called an enhanced for loop ]
    [ January 04, 2007: Message edited by: Nicholas Jordan ]
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    Another advantage of the enhanced for loop is that when you change the type of a variable from a Collection to an array (or vice versa), the code for the loop doesn't have to change.


    The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Colon : in for{} loop?