prefixProductArrayIterator:
public class prefixProductArrayIterator implements java.util.Iterator
the array over which we are iterating
private int array;
the index of the next value to include in the prefix product
private int current;
the product of elements before arraycurrent
private int prefixProduct;
Construct a new prefix product iterator over the given array.
@param a the array containing the values
public prefixProductArrayIteratorint a
array a;
current ;
prefixProduct ;
Return whether there are more elements that have not yet been
part of the prefix product sequence.
@return whether there are more elements that have not yet been
part of the prefix product sequence
@Override
public boolean hasNext
return current array.length;
Return the next value in the prefix product sequence.
@return the next value in the prefix product sequence
@Override
public Integer next
prefixProduct prefixProduct arraycurrent;
current;
return prefixProduct;
main method to test the prefixProductArrayIterator.
@param args size of array to generate
@param args range of random values
public static void mainString args
if argslength
System.err.printlnUsage: java prefixProductArrayIterator size range";
System.exit;
convert the commandline parameters to the numbers needed
int n ;
int range ;
try
n Integer.parseIntargs;
range Integer.parseIntargs;
catch NumberFormatException e
System.err.printlne;
System.exit;
create and populate the array
java.util.Random r new java.util.Random;
int a new intn;
for int i ; i n; i
ai rnextIntrange;
print the array
System.out.printlnGenerated array: java.util.Arrays.toStringa;
print the prefix products
System.out.printlnPrefix productss:";
java.util.Iterator iter new prefixProductArrayIteratora;
while iterhasNext
System.out.printlniternext;