| 
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Objectstructure.AbstractIterator<ELTTYPE>
structure.ArrayIterator<ELTTYPE>
public class ArrayIterator<ELTTYPE>
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.currentTimeMillis();
        for(Iterator i = new ArrayIterator(test); i.hasNext();i.next()){}
        finish = System.currentTimeMillis();
        iteration = finish - start;
        System.out.println("Iteration over array took " + iteration + 
                           " milliseconds to perform.");
        //compute test for looping
        start = System.currentTimeMillis();
        for(int i=0; i<test.length; i++){}
        finish = System.currentTimeMillis();
        looping = finish - start;
        System.out.println("Looping over array took " + (finish-start) + 
                           " milliseconds to perform.");
        
        System.out.println("Iterators are " + (iteration/(double)looping) + " times " +
                           "slower than loops.");
 }
 
| Constructor Summary | |
|---|---|
ArrayIterator(ELTTYPE[] source)
Construct an iterator that iterates over the entire contents of an array.  | 
|
ArrayIterator(ELTTYPE[] source,
              int first,
              int size)
Constructs an iterator that will iterate over a specified portion of the source array.  | 
|
| Method Summary | |
|---|---|
 ELTTYPE | 
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  | 
 ELTTYPE | 
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 structure.AbstractIterator | 
|---|
hasMoreElements, nextElement, remove, value | 
| Methods inherited from class java.lang.Object | 
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait | 
| Constructor Detail | 
|---|
public ArrayIterator(ELTTYPE[] source)
source - The array over which to iterate.
public ArrayIterator(ELTTYPE[] source,
                     int first,
                     int size)
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 | 
|---|
public void reset()
reset in class AbstractIterator<ELTTYPE>public boolean hasNext()
hasNext in interface java.util.Iterator<ELTTYPE>hasNext in class AbstractIterator<ELTTYPE>AbstractIterator.hasMoreElements()public ELTTYPE next()
next in interface java.util.Iterator<ELTTYPE>next in class AbstractIterator<ELTTYPE>AbstractIterator.hasMoreElements(), 
AbstractIterator.value()public ELTTYPE get()
get in class AbstractIterator<ELTTYPE>public static void main(java.lang.String[] argv)
  | 
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||