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.
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:
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.
[ December 24, 2006: Message edited by: Andrew Monkhouse ] [ December 24, 2006: Message edited by: Andrew Monkhouse ]
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com