structure5
Class ArrayIterator<E>

java.lang.Object
  extended by structure5.AbstractIterator<E>
      extended by structure5.ArrayIterator<E>
All Implemented Interfaces:
java.lang.Iterable<E>, java.util.Enumeration<E>, java.util.Iterator<E>

public class ArrayIterator<E>
extends AbstractIterator<E>

A conveniece class that provies a mechanism to iterate over arrays that is analogous to the iteration techniques employed by the structures in this package.

Example Usage:

To prove that loops are faster than iteration we could use the following:

 public static void main(String[] argv){
      //a randomly generated test string
      String testString = "For loops are much faster than iterators";

      //an array over which to iterate
      String[] test = new String[10000000];
      
      //longs to calculate lenght of computation
      long start, finish, iteration, looping;

      //populate test array with our test string
      for(int i=0; i<test.length; i++) test[i] = testString;
      
      //compute test for iteration
      start = System.nanoTime();
      for(Iterator i = new ArrayIterator(test); i.hasNext();i.next()){}
      finish = System.nanoTime();
      iteration = finish - start;
      System.out.println("Iteration over array took " + iteration + 
                         " nanoseconds to perform.");

      //compute test for looping
      start = System.nanoTime();
      for(int i=0; i<test.length; i++){}
      finish = System.nanoTime();
      looping = finish - start;
      System.out.println("Looping over array took " + (finish-start) + 
                         " nanoseconds to perform.");
      
      System.out.println("Iterators are " + (iteration/(double)looping) + " times " +
                         "slower than loops.");
 }
 


Field Summary
protected  int count
          The number of elements in the array over which to iterate
protected  int current
          The current item in the iteration
protected  E[] data
          The array over which this iterator iterates
protected  int head
          The index of the first item in the iteration
protected  int remaining
          The number of items remaining in the itearation
 
Constructor Summary
ArrayIterator(java.lang.Object[] source)
          Construct an iterator that iterates over the entire contents of an array.
ArrayIterator(java.lang.Object[] source, int first, int size)
          Constructs an iterator that will iterate over a specified portion of the source array.
 
Method Summary
 E get()
          Return the object currently specified by the iteration without advancing the iterator to the next object.
 boolean hasNext()
          Returns true iff there are elements specified by our iterator that have not been visited by the current iteration.
static void main(java.lang.String[] argv)
          test code to prove that iterators are slower than for loops
 E next()
          Return the next object in our iteration and advance the iterator to the next object in the iteration.
 void reset()
          Return the iteration to the original state specified by the constructor.
 
Methods inherited from class structure5.AbstractIterator
hasMoreElements, iterator, nextElement, remove, value
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

data

protected E[] data
The array over which this iterator iterates


head

protected int head
The index of the first item in the iteration


count

protected int count
The number of elements in the array over which to iterate


current

protected int current
The current item in the iteration


remaining

protected int remaining
The number of items remaining in the itearation

Constructor Detail

ArrayIterator

public ArrayIterator(java.lang.Object[] source)
Construct an iterator that iterates over the entire contents of an array.

Parameters:
source - The array over which to iterate.

ArrayIterator

public ArrayIterator(java.lang.Object[] source,
                     int first,
                     int size)
Constructs an iterator that will iterate over a specified portion of the source array.

Parameters:
source - The array over which to iterate.
first - The index at which we will start our iteration.
size - The number of elements following that start index that are to be iterated over.
Method Detail

reset

public void reset()
Return the iteration to the original state specified by the constructor.

Specified by:
reset in class AbstractIterator<E>
Postcondition:
The iteration is returned to its original state.

hasNext

public boolean hasNext()
Returns true iff there are elements specified by our iterator that have not been visited by the current iteration.

Specified by:
hasNext in interface java.util.Iterator<E>
Specified by:
hasNext in class AbstractIterator<E>
Returns:
True iff there are elements specified by our iterator that have not been visited by the current iteration.
See Also:
AbstractIterator.hasMoreElements()

next

public E next()
Return the next object in our iteration and advance the iterator to the next object in the iteration.

Specified by:
next in interface java.util.Iterator<E>
Specified by:
next in class AbstractIterator<E>
Returns:
The next object in our iteration.
See Also:
AbstractIterator.hasMoreElements(), AbstractIterator.value()

get

public E get()
Return the object currently specified by the iteration without advancing the iterator to the next object.

Specified by:
get in class AbstractIterator<E>
Returns:
The next object in our iteration.

main

public static void main(java.lang.String[] argv)
test code to prove that iterators are slower than for loops