在 英语 中使用 Iterator 的示例及其翻译为 中文
{-}
-
Political
-
Ecclesiastic
-
Programming
What is an Iterator?
This new iterator is part of the ECMAScript 6.
We have seen that the for statement is such an iterator.
Get value of an iterator, int x=*it;
Iterators all implement a trait named Iterator that is defined in the standard library.
You can get an iterator from that.
An iterator is an object with a next(Python 2) or__next__(Python 3) method.
You can get an iterator from that.
Iterator can be used to traverse Set and List collections whereas ListIterator can only be used with Lists.
And of course you can get an iterator from all three.
At this point the iterator object is exhausted and any further calls to its next() method just raise StopIteration again.
And of course you can get an iterator from all three.
However, when you request an iterator from an iterator(which you would rarely do), it just gives you a copy of itself.
Has a member- or extension-function iterator(), whose return type.
Sequence unpacking also supports iterators: if you know an iterator will return N elements, you can unpack them into an N-tuple:.
The for in construct is able to interact with an Iterator in several ways.
Let's see what an iterator implementation looks like in practice.
For example, consider the following code, which uses an iterator to walk the keys of a hash in Java.
The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order.
A generator is also an iterator, but what is an iterator?
Iterator objects can optionally provide these additional capabilities, but the iterator protocol only specifies the__next__() method.
Iterable should be in a series, an iterator, or various other object that backs iteration.
For example, the list object isan iterable but not an iterator(it implements__iter__ but not next).
Here, we're creating an iterator, using two adaptors, and then consuming the value.
Some interesting disagreement about parts of the above(in particular replacing Iterator usage by access-by-index) has been discussed on Reddit here.
Similarly, if you wish to get a iterator only, you can use Generator Expression with almost the same syntax.
Each Java Collection contains the iteratormethod that returns an Iterator instance.
The incoming method on TcpListener returns an iterator that gives us a sequence of streams(more specifically, streams of type TcpStream).
Therefore, in the face of concurrent modifications, the iterator will soon fail completely without risking any uncertain behavior at any time in the future.