Commit 633a228
committed
feat(ListDemo5): add ListDemo5 demonstrating ArrayList ops and iterators
What
- Added ListDemo5 class demonstrating ArrayList creation, insertion, mutation, and iteration.
- Shows:
- Creating ArrayList with initial capacity.
- Creating ArrayList from List.of(...)
- add(E), add(index, E), addAll(index, Collection)
- set(index, E) to replace elements
- Iteration using Iterator and ListIterator with hasNext()/hasPrevious()
Why
- Educational example that ties together common ArrayList mutation APIs and both iterator types.
- Teaches safe insertion at specific indices, use of addAll at an index to splice collections, and how ListIterator lets you inspect traversal state.
- Useful for learners to observe the difference between forward-only Iterator and bidirectional ListIterator.
How
- Construct al1 with capacity 20 and al2 from List.of(50,60,70,80,90).
- Perform sequence:
- al1.add(10)
- al1.add(0, 5)
- al1.addAll(1, al2)
- al1.set(5, 70)
- al1.set(6, 100)
- Iterate using Iterator<Integer> it = al1.iterator(); printing each element.
- Obtain ListIterator<Integer> it2 = al1.listIterator(); then loop while it2.hasNext(), printing hasPrevious() and hasNext() states.
Logic
- Inputs: two lists of integers; al2 used as source for addAll.
- Outputs:
- Printed sequence of list elements (via Iterator).
- For each position during ListIterator forward traversal, printed iterator state: hasPrevious() and hasNext().
- Flow:
1. Build al1, splice in al2 at index 1.
2. Replace elements at indices 5 and 6.
3. Iterate forward using Iterator to print elements.
4. Iterate forward using ListIterator and print hasPrevious()/hasNext() status at each step.
- Edge cases handled / notes:
- set(index, value) requires index < size; code assumes the previous adds produced sufficient size for indices 5 and 6.
- Iterator is fail-fast; modifying al1 during iteration (outside Iterator methods) would throw ConcurrentModificationException.
- ListIterator can be used for bidirectional traversal and in-place modifications via set/add/remove.
Complexity / performance
- add(E) amortized O(1).
- add(index, E) and addAll(index, Collection) O(n) worst-case due to shifting.
- set(index, E) O(1).
- Iteration O(n).
Real-life applications
- Inserting a batch of elements into an existing ordered list at a specific position.
- Inspecting traversal cursor state in interactive UIs or editors using ListIterator.
- Teaching material for collection mutation vs iteration safety.
Notes
- If you want stable prints of iterator state with values, consider printing the returned value from it2.next() alongside hasPrevious()/hasNext().
- For production code, prefer checking size before calling set(index, ...) to avoid IndexOutOfBoundsException.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 52b97af commit 633a228
File tree
2 files changed
+26
-62
lines changed- Section 25 Collections Frameworks/List Interface/ArrayList/src
- Section25CollectionFramework/src/ListDemo
2 files changed
+26
-62
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 | + | |
This file was deleted.
0 commit comments