Commit d57d6c3
committed
feat(ListIteratorDemo): add demo of bidirectional traversal with ListIterator
What
- Added ListIteratorDemo class.
- Created a List<Integer> with values [5, 10, 50, 60, 70, 80, 90].
- Obtained ListIterator<Integer> from list.listIterator().
- Demonstrated:
- Forward traversal using hasNext() / next().
- Printed each value along with hasPrevious() status.
- Backward traversal using hasPrevious() / previous().
- Printed each value when moving backward.
Why
- Shows how ListIterator extends Iterator with bidirectional navigation.
- Demonstrates practical use of hasPrevious() / previous() in addition to forward traversal.
- Highlights that ListIterator can both move forward and then move back through the same list.
How
- list.listIterator() starts cursor before first element.
- Forward traversal:
- While hasNext() → consume next().
- For each element, print next() result and current hasPrevious() state.
- Backward traversal:
- Once cursor is at end (after forward traversal), hasPrevious() becomes true.
- While hasPrevious() → consume previous().
- Prints values in reverse order.
Logic
- Inputs: list [5, 10, 50, 60, 70, 80, 90].
- Outputs:
- Forward traversal prints numbers in order (5→90).
- Backward traversal prints numbers in reverse (90→5).
- Flow:
1. Forward loop consumes all elements.
2. Iterator ends at list.size().
3. Backward loop consumes elements in reverse until start.
- Edge cases:
- At start, hasPrevious() false.
- At end, hasNext() false.
- Calling next() without hasNext() or previous() without hasPrevious() throws NoSuchElementException.
- Complexity / performance: O(n) traversal forward + backward.
- Concurrency / thread-safety: Fail-fast under structural modification; not thread-safe.
- Error handling: Not needed in demo.
Real-life applications
- Implementing editors, undo/redo features where bidirectional movement is required.
- Navigating playlists, browsing history, or doubly linked data.
- Performing modifications in-place with set(), add(), or remove() during iteration.
Notes
- ListIterator also provides methods to modify the list during traversal.
- More powerful than plain Iterator, but specific to List collections.
- Cursor position moves between elements; after forward traversal, it's positioned after last element.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 480e5ac commit d57d6c3
File tree
1 file changed
+26
-0
lines changed- Section 25 Collections Frameworks/List Interface/ArrayList/Iterator/src
1 file changed
+26
-0
lines changedLines changed: 26 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 | + | |
0 commit comments