On Aug 3, 7:50 pm, JKid314159 <[email protected]> wrote:
> Re: The Java Tutorials
> Home Page>Collections?Interfaces>The Collection Interface
>
> Dear Java Programmer:
>
> I am not too sure how this condition works? Looks simple but I have a block
> on it.
>
> if (!cond(it.next()))
> it.remove();
>
Actually the whole code would be:
static void filter(Collection<?> c) {
for (Iterator<?> it = c.iterator(); it.hasNext(); )
if (!cond(it.next()))
it.remove();
}
(from
http://java.sun.com/docs/books/tutorial/collections/interfaces/collection.html)
So, say you want to get a sub-collection of a collection whose
elements respond to given condition; to do it, you define a filter on
the collection to isolate the elements which responds to the
condition, or to say it more accurately you write a method which
iterates over the collection, and inside this iteration locates the
last element which violates the condition, if it does so you remove
it.
All that, because remove operates on the current element and an
iterator gives a position between two elements, so that it.next gives
you the current element to remove if it violates the condition.
See http://java.sun.com/javase/6/docs/api/java/util/ListIterator.html
for a graphic on cursor position.
> JKid314159http://existentialists.blogspot.com/
>
>
>
--~--~---------~--~----~------------~-------~--~----~
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/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---