T
- The type of the elements in this enumerator.public interface IEnumerator<T> extends IDisposable
Iterator
and Enumeration
.
One difference is that IEnumerator
is read-only. Another
difference is that enumerators provide a method to restart the
iteration over a collection called reset()
.
Other differences are in the semantics of the iteration methods.Modifier and Type | Field and Description |
---|---|
static IEnumerator |
EMPTY
An
IEnumerator that is always empty. |
Modifier and Type | Method and Description |
---|---|
static <T> IEnumerator<T> |
concat(IEnumerator<? extends T> first,
IEnumerator<? extends T> second)
Creates a composite enumerator that enumerates two given enumerators one after the other.
|
T |
current()
Returns the item at the current position of this enumerator.
|
boolean |
moveNext()
Increments the current position by 1.
|
void |
reset()
Resets the position of the pointer to its initial state.
|
default Object[] |
toArray(IEnumerator<T> e,
int size)
Similar to
Collection.toArray() , this method creates
an array that contains the items of the given IEnumerator . |
default <E> E[] |
toArray(IEnumerator<T> e,
int size,
E[] a)
Similar to
Collection.toArray(Object[]) , this method creates
an array that contains the items of the given IEnumerator . |
close, dispose
static final IEnumerator EMPTY
IEnumerator
that is always empty.static <T> IEnumerator<T> concat(IEnumerator<? extends T> first, IEnumerator<? extends T> second)
first
- The first one.second
- The second one.IEnumerator
which enumerate of the first enumerator and then over the second.T current()
Calling this method does not move the pointer. Thus, it is possible to retrieve the same item multiple times with subsequent calls to this method.
Important: The initial position of the pointer
is before the first item (equal to -1), so moveNext()
must be called prior to this method at least once.
If this method is called before an initial moveNext()
,
the result of this method is undefined or it may even throw
an exception.
boolean moveNext()
void reset()
current()
, moveNext()
must be called after calling this method.default Object[] toArray(IEnumerator<T> e, int size)
Collection.toArray()
, this method creates
an array that contains the items of the given IEnumerator
.
Note:
In contrary to the counterpart defined in Collection
, this
method throws a ClassCastException
if T
cannot be cast to E
instead of an ArrayStoreException
.
ClassCastException
- if T
cannot be cast to E
ArrayIndexOutOfBoundsException
- if size
is smaller than the actual size of the enumeratorsize
- The size of the enumeratorsize
big and contains all elements of the enumerator.default <E> E[] toArray(IEnumerator<T> e, int size, E[] a)
Collection.toArray(Object[])
, this method creates
an array that contains the items of the given IEnumerator
.
Note:
In contrary to the counterpart defined in Collection
, this
method throws a ClassCastException
if T
cannot be cast to E
instead of an ArrayStoreException
.
ClassCastException
- if T
cannot be cast to E
ArrayIndexOutOfBoundsException
- if size
is smaller than the actual size of the enumeratorsize
- The size of the enumeratora
- The array to store the items in. Note: If the array is to small, then a new array is created and returned.size
big and contains all elements of the enumerator.