On 6 January 2011 11:47, Carl Jokl <[email protected]> wrote:
> 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();
> }
>
>
there's loads of ways to do this without an index
scala:
//mnemonics will be an Array[Char]
val mnemonics = Type.values map { _.getMnemonic }
lambdaj:
//no support for Arrays, so it'll have to be a list, which means primitives
can't be stored either
final List<Type> typeList = Arrays.asList(Type.values())
final List<Character> mnemonicList = convert(typeList, new
PropertyExtractor("mnemonic"));
//this needs to work, or we're back to a loop for Character unboxing
final char[] mnemonics = mnemonicList.toArray(new char[0])
google collections:
//no support for Arrays, etc, etc.
final Function<Type, Character> lookupMnemonic = new Function<Type,
Character> {
Character apply(Type tpe) { return tpe.getMemonic }
}
final List<Type> typeList = Arrays.asList(Type.values())
final List<Character> mnemonicList = Collections2.transform(typeList,
lookupMnemonic)
final char[] mnemonics = mnemonicList.toArray(new char[0])
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]<javaposse%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/javaposse?hl=en.
>
>
--
Kevin Wright
gtalk / msn : [email protected]
<[email protected]>mail: [email protected]
vibe / skype: kev.lee.wright
twitter: @thecoda
--
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.