RuneHive-Game
Loading...
Searching...
No Matches
com.runehive.util.ArrayIterator< E > Class Template Reference

An Iterator implementation that will iterate over the elements in an array without the overhead of the ArrayLists withdraw operation. More...

Inheritance diagram for com.runehive.util.ArrayIterator< E >:
Collaboration diagram for com.runehive.util.ArrayIterator< E >:

Public Member Functions

 ArrayIterator (E[] array)
 Creates a new ArrayIterator.
boolean hasNext ()
next ()
peek ()
void remove ()

Private Attributes

final E[] array
 The array that is storing the elements.
int index
 The current index that the iterator is iterating over.
int lastIndex = -1
 The last index that the iterator iterated over.

Detailed Description

An Iterator implementation that will iterate over the elements in an array without the overhead of the ArrayLists withdraw operation.

Parameters
<E>the type of array being iterated over.
Author
lare96 http://github.com/lare96

Definition at line 15 of file ArrayIterator.java.

Constructor & Destructor Documentation

◆ ArrayIterator()

Creates a new ArrayIterator.

Parameters
arraythe array that is storing the elements.

Definition at line 36 of file ArrayIterator.java.

36 {
37 this.array = array;
38 }

References array.

Member Function Documentation

◆ hasNext()

boolean com.runehive.util.ArrayIterator< E >.hasNext ( )

Definition at line 41 of file ArrayIterator.java.

41 {
42 return !(index + 1 > array.length);
43 }
val index

References array, and index.

◆ next()

Definition at line 51 of file ArrayIterator.java.

51 {
52 if(index >= array.length)
53 throw new ArrayIndexOutOfBoundsException("There are no elements left to iterate over!");
54 lastIndex = index;
55 index++;
56 return array[lastIndex];
57 }

References array, index, and lastIndex.

◆ peek()

Definition at line 46 of file ArrayIterator.java.

46 {
47 return array[index];
48 }

References array, and index.

◆ remove()

void com.runehive.util.ArrayIterator< E >.remove ( )

Definition at line 60 of file ArrayIterator.java.

60 {
61 if(lastIndex == -1)
62 throw new IllegalStateException("This method can only be called once after \"next\".");
63 array[lastIndex] = null;
64 lastIndex = -1;
65 }

References array, and lastIndex.

Member Data Documentation

◆ array

final E [] com.runehive.util.ArrayIterator< E >.array
private

The array that is storing the elements.

Definition at line 20 of file ArrayIterator.java.

Referenced by ArrayIterator(), hasNext(), next(), peek(), and remove().

◆ index

int com.runehive.util.ArrayIterator< E >.index
private

The current index that the iterator is iterating over.

Definition at line 25 of file ArrayIterator.java.

Referenced by hasNext(), next(), and peek().

◆ lastIndex

int com.runehive.util.ArrayIterator< E >.lastIndex = -1
private

The last index that the iterator iterated over.

Definition at line 30 of file ArrayIterator.java.

Referenced by next(), and remove().


The documentation for this class was generated from the following file: