1 sportbilly 1.1.2.1 // ArrayIterator.java, created Tue Jun 15 15:59:04 1999 by cananian 2 sportbilly 1.1.2.1 // Copyright (C) 1998 C. Scott Ananian <cananian@alumni.princeton.edu> 3 sportbilly 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 sportbilly 1.1.2.1 package harpoon.Util; 5 sportbilly 1.1.2.1 6 cananian 1.6 import java.util.ListIterator; 7 sportbilly 1.1.2.1 import java.util.NoSuchElementException; 8 sportbilly 1.1.2.1 /** 9 sportbilly 1.1.2.1 * An <code>ArrayIterator</code> iterates over the elements of an array. 10 cananian 1.6 * <p>The <code>add()</code>, <code>set()</code> and <code>remove()</code> 11 cananian 1.6 * methods are <b>not</b> implemented; this iterator is unmodifiable. 12 sportbilly 1.1.2.1 * 13 sportbilly 1.1.2.1 * @author C. Scott Ananian <cananian@alumni.princeton.edu> 14 cananian 1.6 * @version $Id: ArrayIterator.java,v 1.6 2004/02/07 19:26:09 cananian Exp $ 15 sportbilly 1.1.2.1 */ 16 sportbilly 1.1.2.1 17 cananian 1.6 public class ArrayIterator<E> implements ListIterator<E> { 18 cananian 1.3.2.2 final E[] oa; 19 sportbilly 1.1.2.1 int i = 0; 20 sportbilly 1.1.2.1 21 cananian 1.3.2.3 /** Creates an <code>ArrayIterator</code> that iterates over the 22 cananian 1.3.2.3 * contents of <code>oa</code>. */ 23 cananian 1.3.2.2 public ArrayIterator(E[] oa) { 24 cananian 1.3.2.1 assert oa!=null; 25 sportbilly 1.1.2.1 this.oa = oa; 26 sportbilly 1.1.2.1 } 27 sportbilly 1.1.2.1 public boolean hasNext() { return ( i < oa.length ); } 28 cananian 1.3.2.2 public E next() { 29 sportbilly 1.1.2.1 if (i < oa.length) 30 sportbilly 1.1.2.1 return oa[i++]; 31 sportbilly 1.1.2.1 else 32 sportbilly 1.1.2.1 throw new NoSuchElementException(); 33 cananian 1.6 } 34 cananian 1.6 public int nextIndex() { return i; } 35 cananian 1.6 36 cananian 1.6 public boolean hasPrevious() { return ( i > 0 ); } 37 cananian 1.6 public E previous() { 38 cananian 1.6 if (i > 0) 39 cananian 1.6 return oa[--i]; 40 cananian 1.6 else 41 cananian 1.6 throw new NoSuchElementException(); 42 cananian 1.6 } 43 cananian 1.6 public int previousIndex() { return i-1; } 44 cananian 1.6 45 cananian 1.6 /* unmodifiable */ 46 cananian 1.6 public void add(E o) { 47 cananian 1.6 throw new UnsupportedOperationException(); 48 cananian 1.6 } 49 cananian 1.6 public void remove() { 50 cananian 1.6 throw new UnsupportedOperationException(); 51 cananian 1.6 } 52 cananian 1.6 public void set(E o) { 53 cananian 1.6 throw new UnsupportedOperationException(); 54 sportbilly 1.1.2.1 } 55 cananian 1.2 }