You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Constructs a new iterator for the dynamic array.
195
+
*/
160
196
DynamicArrayIterator() {
161
197
this.expectedModCount = modCount;
162
198
}
163
199
200
+
/**
201
+
* Checks if there are more elements in the iteration.
202
+
*
203
+
* @return true if there are more elements, false otherwise
204
+
*/
164
205
@Override
165
206
publicbooleanhasNext() {
166
207
checkForComodification();
167
208
returncursor < size;
168
209
}
169
210
211
+
/**
212
+
* Returns the next element in the iteration.
213
+
*
214
+
* @return the next element in the iteration
215
+
* @throws NoSuchElementException if the iteration has no more elements
216
+
*/
170
217
@Override
171
218
@SuppressWarnings("unchecked")
172
219
publicEnext() {
@@ -177,22 +224,38 @@ public E next() {
177
224
return (E) elements[cursor++];
178
225
}
179
226
227
+
/**
228
+
* Removes the last element returned by this iterator.
229
+
*
230
+
* @throws IllegalStateException if the next method has not yet been called, or the remove method has already been called after the last call to the next method
231
+
*/
180
232
@Override
181
233
publicvoidremove() {
182
234
if (cursor <= 0) {
183
-
thrownewIllegalStateException();
235
+
thrownewIllegalStateException("Cannot remove element before calling next()");
184
236
}
185
237
checkForComodification();
186
238
DynamicArray.this.remove(--cursor);
187
-
expectedModCount = ++modCount;
239
+
expectedModCount = modCount;
188
240
}
189
241
242
+
/**
243
+
* Checks for concurrent modifications to the array during iteration.
244
+
*
245
+
* @throws ConcurrentModificationException if the array has been modified structurally
246
+
*/
190
247
privatevoidcheckForComodification() {
191
248
if (modCount != expectedModCount) {
192
249
thrownewConcurrentModificationException();
193
250
}
194
251
}
195
252
253
+
/**
254
+
* Performs the given action for each remaining element in the iterator until all elements have been processed.
255
+
*
256
+
* @param action the action to be performed for each element
257
+
* @throws NullPointerException if the specified action is null
0 commit comments