PROPOSAL: Enhanced for each loop iteration control
Stephen Colebourne
scolebourne at joda.org
Fri May 1 01:16:25 PDT 2009
2009/5/1 Derek Foster <vapor1 at teleport.com>:
> Firstly, I have very rarely needed to call Iterator.delete() in a loop. (I can think of maybe four or five occasions.) As such, it has usually not been a great deal of trouble to just write the loop using an explicit iterator.
Thanks for the numeric feedback. It may depend on coding style. 5% of
the loops in the enterprise system I work on have remove().
> On the other hand, I much more frequently would like to be able to use an index.
And very few have an index ;-)
>However, the proposal seems to assume that if a user wants either an iterator or an index, that the user wants both. In practice, I usually want either one OR the other, but pretty much never both. (For instance, Iterators are good for iterating LinkedLists, and indices are good for ArrayLists, but using the wrong one on a list of any significant size will create a performance problem.)
The existing enhanced for-each loop uses Iterators for ArrayList.
> I would much prefer to have a simpler syntax that allowed me to work just with an index or just with an iterator... Actually, I'd be happy if just indices were supported. Something like:
>
> Thing[] fooCollection = ...whatever...;
> String[] results = new String[fooCollection.length];
> for (Thing foo : fooCollection) {
> results[#foo] = foo.toString();
> }
>
> where "#foo" was defined by the compiler as "the index of foo within the collection it is iterating over".
#foo has a meaning in FCM as a field reference, and similar syntax was
adopted by BGGA (unpublished as a document).
It should also be noted that this approach breaks on nested loops (its brittle).
Stephen
More information about the coin-dev
mailing list