Re: array to iterator

From: Boudewijn Dijkstra (usenet_at_bdijkstra.tmfweb.nl)
Date: 05/18/04


Date: Tue, 18 May 2004 22:48:23 +0200


"Roedy Green" <look-on@mindprod.com.invalid> schreef in bericht
news:v8dba0pa801cthcebrjubt2cmbjd8ub5j7@4ax.com...
> Is there a more efficient way to turn an array of Strings into an
> Iterator of Strings?
>
> FilenameFilter f = new ClamFilter( "", ".html" );
> String [] fileNames = new File( "E:/mindprod/ggloss" ).list( f );
> ArrayList toProcess = new ArrayList( Arrays.asList( fileNames) );
> Iterator fileIterator = toProcess.iterator();

How about an ArrayIterator?

import java.util.*;

/**
 * Iterator for iterating over an array of <code>Object</code>s.
 * The {@link #remove()} and {@link #add(Object)} methods are unsupported,
 * use an ArrayList for these operations.
 */
public class ArrayIterator
 implements ListIterator
{
 /** The array over which is being iterated. */
 protected Object[] array;
 /** The length of the array is buffered here. */
 protected int length;
 /**
  * This member is used to keep track of the cursor of the iterator.
  * When iterating, it is used to obtain an index into the array.
  */
 protected int cursor;
 /** The index of the last returned element is stored here, for use in
set(Object). */
 protected int lastIndex;

 public ArrayIterator(Object[] array)
 {
  this.array = array;
  length = array.length;
  cursor = -1; // don't know initial direction
  lastIndex = -1;
 }

 public boolean hasNext()
 {
  if (cursor == -1)
   return length > 0;
  return cursor < length;
 }

 public Object next()
 {
  int cursor = this.cursor;
  if (cursor == -1)
   cursor = 0;
  try {
   Object o = array[cursor];
   lastIndex = cursor;
   this.cursor = cursor + 1; // post-increment
   return o;
  } catch (ArrayIndexOutOfBoundsException aioobe) {
   NoSuchElementException rethrow = new
NoSuchElementException("ArrayIndexOutOfBoundsException");
   rethrow.initCause(aioobe);
   throw rethrow;
  }
 }

 public boolean hasPrevious()
 {
  if (cursor == -1)
   return length > 0;
  return cursor > 0;
 }

 public Object previous()
 {
  int cursor = this.cursor;
  if (cursor == -1)
   cursor = length;
  try {
   Object o = array[--cursor]; // pre-decrement
   lastIndex = cursor;
   this.cursor = cursor;
   return o;
  } catch (ArrayIndexOutOfBoundsException aioobe) {
   NoSuchElementException rethrow = new
NoSuchElementException("ArrayIndexOutOfBoundsException");
   rethrow.initCause(aioobe);
   throw rethrow;
  }
 }

 public int nextIndex() {
  return lastIndex + 1;
 }

 public int previousIndex() {
  return lastIndex - 1;
 }

 // unsupported
 public void remove() {
  throw new UnsupportedOperationException("Size of array is fixed");
 }

 public void set(Object o)
 {
  try {
   array[lastIndex] = o;
  } catch (ArrayIndexOutOfBoundsException aioobe) {
   IllegalStateException rethrow = new IllegalStateException("Neither next()
nor previous() have been called");
   rethrow.initCause(aioobe);
   throw rethrow;
  }
 }

 // unsupported
 public void add(Object o) {
  throw new UnsupportedOperationException("Size of array is fixed");
 }
}

For the die-hards I also have a PrimitiveArrayIterator, that iterates over
any array with primitive-typed elements. But that has nothing to do with
efficiency. :)



Relevant Pages

  • Re: Improved for each loop
    ... Objectarray .... ... private final int start; ... public Iterator<Integer> iterator{ ... The new syntax would be pure syntactic sugar - it wouldn't require any new classes, or bytecodes, or class format changes, or changes to any other part of the language. ...
    (comp.lang.java.programmer)
  • Re: Validating data, tricky one...
    ... It might be easier to create this table as a cursor with a query: ... CREATE CURSOR x1 (number int) ... INSERT INTO x1 (number) SELECT num FROM xx WHERE RECNO=1 ... Or put them into an array and sort the array starting with element 2 using ...
    (microsoft.public.fox.programmer.exchange)
  • (patch for Bash) regex case statement
    ... Following up on my previous patch for regex conditional tests, ... /* Return an array of strings; ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: Strategy or Iterator?
    ... It would be possible to write a class that returns the variations ... GNU General Public License for more details. ... protected CombinatoricOperator(Telements, int r) { ... An integer array backing up the original one to keep track of the ...
    (comp.lang.java.programmer)
  • (patch for Bash) regex conditional tests
    ... 'regex' are returned in array variable SUBMATCH. ... Skipping of positional parameters, array elements, string ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)