I've noticed that Java's Iterator
(interface) is similar to C#'s IEnumerable
, but is there any way to use it like this:
private IEnumerable<Label> it;
it = labels.iterator();
In Java I could just do:
private Iterator<JLabel> it;
it = labels.iterator();
What's the C# equivalent of Java's Iterator
interface?
Iterator and for-each loop are faster than simple for loop for collections with no random access, while in collections which allows random access there is no performance change with for-each loop/for loop/iterator.
Generally, an iterator in Java is used to loop through any collection of objects. To apply the iterator, all you need to do is import the java. util package and then use the iterator() method. You can then use the iterator to perform multiple operations in the collection.
An iterator is an object that allows you to step through the contents of another object, by providing convenient operations for getting the first element, testing when you are done, and getting the next element if you are not. In C, we try to design iterators to have operations that fit well in the top of a for loop.
To determine whether to use loops or iterators, you need to know which implementation is faster: the version of the search function with an explicit for loop or the version with iterators. The iterator version was slightly faster!
It's not used very often, but the analogy is the IEnumerator<T>
interface:
var enumerator = labels.GetEnumerator();
.NET's IEnumerator
differs from Java's Iterator
with the following:
Iterator
after construction is pointing at the first element of the collection (or, for an empty collection, is invalid and hasNext
will return false
immediately), IEnumerator
points initially before the first element of the collection (for an empty collection MoveNext
will return false
) Iterator
has hasNext
method, while for IEnumerator
you verify the result of MoveNext
methodIterator
has next
method, while for IEnumerator
you also use MoveNext
Iterator
's next
returns the next element, while with IEnumerator
you use Current
property after calling MoveNext
Iterator
in Java has remove
method which allows you to remove elements from the underlying collection. There is no equivalent in IEnumerator
So for Java you'd iterate with something like this:
it = labels.iterator();
while (it.hasNext())
{
elem = it.next();
}
While in C#:
en = labels.GetEnumerator();
while (en.MoveNext())
{
elem = en.Current;
}
Usually, having labels
as a collection (which always implements IEnumerable<T>
) you just use it directly:
foreach (var label in labels)
{
//...
}
And of course, you can store IEnumerable<T>
for later use (names referring to your example):
IEnumerable<Label> it = labels;
Beware, that IEnumerable<T>
is lazy, just like Iterator
in Java.
You can also easily obtain a snapshot of a collection like this (again, it
refers to your example, better name could be chosen):
IEnumerable<Label> it = labels.ToArray();
// or Label[] it = labels.ToArray();
// or better: var it = labels.ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With