Re: foreach and stack, iterating from the bottom-up?



Andreas Leitgeb wrote:
Piotr Kobzda <pikob@xxxxxxxxx> wrote:
It's not an actual
problem, I simply adapted my logic in the method to this behavior,
but, as I said, I was surprised.
It's possible to achieve what you want, ...

If the OP was using a sufficiently new Java (>=1.6), then he
could use method "descendingIterator()" offered by a couple
of the collection classes/interfaces. (Deque,NavigableSet,
LinkedList,TreeSet,...)

the descendingIterator() is another argument in favour of
allowing for-loops with iterators, not just iterables.

For loops are allowed with iterators. The enhanced for loop is syntactic sugar designed for one common use, only.

For the time being (since "for" does not directly work with
iterators) the following short but ugly code works around:

Now, don't go spreading vicious rumors. The 'for' construct works perfectly well, and no need to use ugliness like

for (Integer i: new Iterable<Integer>() { public Iterator<Integer> iterator() { return dequeVar.descendingIterator(); } } )
{ ... loop body ... }

Just do:

for ( Iterator <Foo> iter = deq.descendingIterator(); iter.hasNext(); )
{
Foo foo = iter.next();
doSomethingWith( foo );
}

Now isn't that much cleaner? And without scaring the people with falsehoods about 'for' loops, too.

--
Lew
.



Relevant Pages

  • Re: < versus !=, was re: i++ vs. ++i
    ... >> is most appropriate in for loops. ... The latter can be done for random access iterators, ... the article was not leaning towards the efficiency aspect at ... using an unsigned integer. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Iterator and arrays
    ... >> I've a problem I'm not able to solve with iterators and to ... Just name your struct 'foo' and move on. ... > a pointer to 'foo'? ... > container stores objects. ...
    (comp.lang.cpp)
  • Re: Maximum number of bits
    ... I posted contents of a lookup table with 17 rows to cover the first set of combinations. ... The "nested loops within loops" refers to a procedural looping solution. ... CREATE TABLE Foo; ... > management and physical I/O (unless you have 40 GB of RAM dedicated to ...
    (microsoft.public.sqlserver.programming)
  • Re: strange crash after assertion with std::map::iterator
    ... in general that foo f is equivalent to foo f = foo. ... the standard doesn't, but iterators have VALUE semantics (as, say, ... strings), ... Such a requirement would rule out raw pointers as valid iterators. ...
    (microsoft.public.vc.stl)
  • Re: RfD: rev 1 of TRAVERSE-WORDLIST proposal
    ... control data for DO loops is not on the data stack while executing the ... care that Forth compilers expend on DO loops. ... all iterators, probably not even the most frequently used ones. ... M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html ...
    (comp.lang.forth)