Skip to content

Commit 633a228

Browse files
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

2 files changed

+26
-62
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.*;
2+
3+
public class ListDemo5 {
4+
public static void main(String[] args) {
5+
ArrayList<Integer> al1 = new ArrayList<>(20);
6+
ArrayList<Integer> al2 = new ArrayList<>(List.of(50, 60, 70, 80, 90));
7+
8+
al1.add(10);
9+
al1.add(0,5);
10+
al1.addAll(1, al2);
11+
al1.set(5,70);
12+
al1.set(6, 100);
13+
14+
Iterator<Integer> it=al1.iterator();
15+
while(it.hasNext()) {
16+
System.out.println(it.next());
17+
}
18+
19+
ListIterator<Integer> it2 = al1.listIterator();
20+
21+
while(it2.hasNext()) {
22+
System.out.println(it2.hasPrevious());
23+
System.out.println(it2.hasNext());
24+
}
25+
}
26+
}

Section25CollectionFramework/src/ListDemo/ListDemo2.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)