1 18 19 package com.Ostermiller.util; 20 21 import java.util.*; 22 23 32 public class ArrayIterator <ElementType> implements Iterator { 33 34 37 private ElementType[] array; 38 39 42 private int index = 0; 43 44 47 private boolean lastRemoved = false; 48 49 56 public ArrayIterator(ElementType[] array){ 57 this.array = array; 58 } 59 60 68 public boolean hasNext(){ 69 return (index < array.length); 70 } 71 72 81 public ElementType next() throws NoSuchElementException { 82 if (index >= array.length) throw new NoSuchElementException("Array index: " + index); 83 ElementType object = array[index]; 84 index++; 85 lastRemoved = false; 86 return object; 87 } 88 89 98 public void remove(){ 99 if (index == 0) throw new IllegalStateException (); 100 if (lastRemoved) throw new IllegalStateException (); 101 array[index-1] = null; 102 lastRemoved = true; 103 } 104 } | Popular Tags |