Java Reference
In-Depth Information
1 /**
2 * This is the implementation of the HashSetIterator.
3 * It maintains a notion of a current position and of
4 * course the implicit reference to the HashSet.
5 */
6 private class HashSetIterator implements Iterator<AnyType>
7 {
8 private int expectedModCount = modCount;
9 private int currentPos = -1;
10 private int visited = 0;
11
12 public boolean hasNext( )
13 {
14 if( expectedModCount != modCount )
15 throw new ConcurrentModificationException( );
16
17 return visited != size( );
18 }
19
20 public AnyType next( )
21 {
22 if( !hasNext( ) )
23 throw new NoSuchElementException( );
24
25 do
26 {
27 currentPos++;
28 } while( currentPos < array.length &&
29 !isActive( array, currentPos ) );
30
31 visited++;
32 return (AnyType) array[ currentPos ].element;
33 }
34
35 public void remove( )
36 {
37 if( expectedModCount != modCount )
38 throw new ConcurrentModificationException( );
39 if( currentPos == -1 || !isActive( array, currentPos ) )
40 throw new IllegalStateException( );
41
42 array[ currentPos ].isActive = false;
43 currentSize--;
44 visited--;
45 modCount++;
46 expectedModCount++;
47 }
48 }
figure 20.18
The HashSetIterator
inner class
Search WWH ::




Custom Search