Skip to content

Commit d57d6c3

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

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
import java.util.ListIterator;
4+
5+
/* Demonstrates ListIterator, which supports bidirectional movement and
6+
methods like hasPrevious() / previous() in addition to next().
7+
*/
8+
9+
public class ListIteratorDemo {
10+
public static void main(String[] args) {
11+
List<Integer> list = new ArrayList<>(List.of(5, 10, 50, 60, 70, 80, 90));
12+
13+
ListIterator<Integer> it = list.listIterator();
14+
15+
System.out.println("Forward traversal using ListIterator:");
16+
while (it.hasNext()) {
17+
Integer val = it.next();
18+
System.out.println("next(): " + val + " | hasPrevious(): " + it.hasPrevious());
19+
}
20+
21+
System.out.println("\nBackward traversal using ListIterator:");
22+
while (it.hasPrevious()) {
23+
System.out.println("previous(): " + it.previous());
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)