Commit 480e5ac
committed
feat(IteratorForEachLoop): add demo comparing for-each, Iterator, and ListIterator
What
- Added IteratorForEachLoop class with examples of three traversal techniques:
1. For-each loop:
- Iterates list of integers [1,2,3,4,5].
- Prints each element with "For-each element: ".
2. Iterator:
- Demonstrates forward traversal with hasNext()/next().
- Creates separate list "numbers" and removes even numbers safely with itr.remove().
- Prints result after removals → [1, 3, 5].
3. ListIterator:
- Demonstrates bidirectional iterator.
- Iterates "numbers" with hasNext()/next().
- Shows potential to modify elements with set() or add().
Why
- Explains trade-offs between different iteration techniques.
- Demonstrates safe removal of elements during iteration (Iterator.remove()).
- Shows enhanced functionality of ListIterator for bidirectional traversal and element modification.
- Highlights why for-each cannot safely remove elements.
How
- For-each loop over list: syntactic sugar over Iterator, read-only.
- Iterator:
- numbers.iterator() returns forward-only cursor.
- While loop checks hasNext(), consumes next().
- Even numbers removed safely via itr.remove().
- ListIterator:
- numbers.listIterator() allows traversal in both directions.
- Iterates forward in demo, prints values.
- Could update elements using set(), add(), remove().
Logic
- Inputs:
- list = [1,2,3,4,5].
- numbers = [1,2,3,4,5].
- Outputs:
- For-each prints 1–5.
- Iterator prints 1–5, then removes evens, leaving [1,3,5].
- ListIterator prints 1,3,5.
- Flow:
1. For-each demonstrates basic traversal.
2. Iterator shows safe removal in traversal.
3. ListIterator highlights richer API (bidirectional, modifications).
- Edge cases:
- Removing in for-each causes ConcurrentModificationException.
- Calling next()/previous() without hasNext()/hasPrevious() throws NoSuchElementException.
- Complexity / performance: O(n) for all traversals.
- Concurrency / thread-safety: Iterators are fail-fast; not safe with concurrent modification outside iterator.
- Error handling: Not needed in demo.
Real-life applications
- For-each: simple reading (printing, aggregating).
- Iterator: filtering/removing unwanted elements during traversal.
- ListIterator: implementing editors, undo/redo stacks, or interactive traversals where moving both forward/backward is required.
Notes
- For-each is concise, but limited.
- Iterator is the most common low-level API for traversal + safe removal.
- ListIterator is list-specific, with powerful bidirectional operations.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 2453780 commit 480e5ac
File tree
1 file changed
+105
-0
lines changed- Section 25 Collections Frameworks/List Interface/ArrayList/Iterator/src
1 file changed
+105
-0
lines changedLines changed: 105 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
0 commit comments