Write IteratorLab. Read a limit, then integers into an ArrayList<Integer>.
- On a copy, try the wrong way: remove the values greater than the limit inside a for-each loop. Catch
ConcurrentModificationExceptionand print which happened (the exception, or — by luck — no exception and the resulting list). - On the original, remove them correctly with
Iterator.remove()and print how many were removed. - Copy the result into a
LinkedListand walk it with aListIterator: replace each negative value by 0 (set), and insertvalue + 1right after each even non-negative value (add). - With the same iterator, walk backwards (
hasPrevious,previous) and print the values.
For the input
10 4 15 8 -3 22 6
the program prints
start: [4, 15, 8, -3, 22, 6]
for-each removal: ConcurrentModificationException
removed 2 values above 10: [4, 8, -3, 6]
after set/add: [4, 5, 8, 9, 0, 6, 7]
backwards: 7 6 0 9 8 5 4