Java Reference
In-Depth Information
136 public AnyType next( )
137 {
138 if( !hasNext( ) )
139 throw new NoSuchElementException( );
140 nextCompleted = true;
141 prevCompleted = false;
142 return theItems[ current++ ];
143 }
144
145 public AnyType previous( )
146 {
147 if( !hasPrevious( ) )
148 throw new NoSuchElementException( );
149 prevCompleted = true;
150 nextCompleted = false;
151 return theItems[ --current ];
152 }
153
154 public void remove( )
155 {
156 if( expectedModCount != modCount )
157 throw new ConcurrentModificationException( );
158
159 if( nextCompleted )
160 ArrayList.this.remove( --current );
161 else if( prevCompleted )
162 ArrayList.this.remove( current );
163 else
164 throw new IllegalStateException( );
165
166 prevCompleted = nextCompleted = false;
167 expectedModCount++;
168 }
169 }
170 }
figure 15.16
ArrayList
implementation
(part 4)
The ArrayListIterator class is completed in Figure 15.16. next and previous
are mirror image symmetries. Examining next , we see first a test at line 138 to
make sure we have not exhausted the iteration (implicitly this tests for structural
modifications also). We then set nextCompleted to true to allow remove to succeed,
and then we return the array item that current is examining, advancing current
after its value has been used.
The previous method is similar, except that we must lower current 's value
first. This is because when traversing in reverse, if current equals the con-
tainer size, we have not yet started the iteration, and when current equals
zero, we have completed the iteration (but can remove the item in this position
Search WWH ::




Custom Search