One item which I have heard mention recently is that the for loop
syntax is potentially harmful to teach because the for loop with an
index is inherently only processable with a single thread vs the
foreach loop which can potentially use parallel processing.
I use mostly foreach loops but there is one situation in Java where I
seem stuck with using the indexed version. This may be a shortcoming
in Java because the order in which iteration is done does not matter.
The situation is when doing operations which involve copying or
deriving values from one array to another array. The foreach loop in
Java is fine for taking values out of an array but I don't think as
far as I have been able to find out
that these loops can be used for receiving values. In this case the
for loop with the index is used to get an element out of an array at a
given index and the derived value is stored in the destination array
at the same index.
For example:
Type[] types = Type.values();
char[] mnemonics = char[types.length];
for (int index = 0; index < mnemonics.length; index++) {
mnemonics[index] = types[index].getMnemonic();
}
I come across this situation a fair amount but don't think it can be
done in foreach style in Java though I think some kind of closure in
other languages would allow the source to be mapped to the destination
as I understand it.
--
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.