Re: foreach and stack, iterating from the bottom-up?
- From: Lew <lew@xxxxxxxxxxxxx>
- Date: Tue, 06 May 2008 06:39:20 -0400
Andreas Leitgeb wrote:
Piotr Kobzda <pikob@xxxxxxxxx> wrote:It's not an actualIt's possible to achieve what you want, ...
problem, I simply adapted my logic in the method to this behavior,
but, as I said, I was surprised.
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
.
- Follow-Ups:
- Re: foreach and stack, iterating from the bottom-up?
- From: Andreas Leitgeb
- Re: foreach and stack, iterating from the bottom-up?
- References:
- foreach and stack, iterating from the bottom-up?
- From: WP
- Re: foreach and stack, iterating from the bottom-up?
- From: Piotr Kobzda
- Re: foreach and stack, iterating from the bottom-up?
- From: Andreas Leitgeb
- foreach and stack, iterating from the bottom-up?
- Prev by Date: Re: JMX Connection Problems
- Next by Date: Re: Use of Taglib in JSP
- Previous by thread: Re: foreach and stack, iterating from the bottom-up?
- Next by thread: Re: foreach and stack, iterating from the bottom-up?
- Index(es):
Relevant Pages
|