Java Reference
In-Depth Information
16 }
17
18 // post: returns true if there are more elements left
19 public boolean hasNext() {
20 return position < list.size();
21 }
22
23 // pre : hasNext() (throws NoSuchElementException if not)
24 // post: returns the next element in the iteration
25 public int next() {
26 if (!hasNext()) {
27 throw new NoSuchElementException();
28 }
29 int result = list.get(position);
30 position++;
31 removeOK = true;
32 return result;
33 }
34
35 // pre : next() has been called without a call on remove
36 // (throws IllegalStateException if not)
37 // post: removes the last element returned by the iterator
38 public void remove() {
39 if (!removeOK) {
40 throw new IllegalStateException();
41 }
42 list.remove(position - 1);
43 position--;
44 removeOK = false;
45 }
46 }
The program imports the java.util package because the class for one of the excep-
tions we want to throw, NoSuchElementException , comes from that package.
You also have to modify the ArrayIntList class. Remember that it needs to have
a method called iterator that constructs an iterator, which means it will look like
this:
public ArrayIntListIterator iterator() {
return new ArrayIntListIterator(...);
}
So which list should you mention in the call on the ArrayIntListIterator
constructor? The ArrayIntList is supposed to construct an iterator that is looking at
 
Search WWH ::




Custom Search