This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Colon : in for{} loop?

 
Jon Campbell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11877
195
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic