Week 8: Java – ListIterator & LinkedList
Q1. MarksUpdater – Using ListIterator with ArrayList
Objective: Maintain a list of student marks and manipulate it using a ListIterator.
import java.util.*;
class MarksUpdater {
private List<String> studentMarks = new ArrayList<>();
private ListIterator<String> iterator;
private boolean canRemoveOrSet = false;
public void initializeIterator() {
iterator = studentMarks.listIterator();
canRemoveOrSet = false;
}
public void nextEntry() {
if (iterator.hasNext()) {
System.out.println("Cursor at: " + iterator.next());
canRemoveOrSet = true;
} else System.out.println("Already at the end of the list.");
}
public void previousEntry() {
if (iterator.hasPrevious()) {
System.out.println("Cursor at: " + iterator.previous());
canRemoveOrSet = true;
} else System.out.println("Already at the beginning of the list.");
}
public void setEntry(String entry) {
if (canRemoveOrSet) {
iterator.set(entry);
System.out.println("Updated current to: " + entry);
} else System.out.println("Cannot set. Move next or previous first.");
}
public void addEntry(String entry) {
iterator.add(entry);
System.out.println("Added: " + entry);
canRemoveOrSet = false;
}
public void removeEntry() {
if (canRemoveOrSet) {
iterator.remove();
System.out.println("Removed last returned entry.");
canRemoveOrSet = false;
} else System.out.println("Cannot remove. Move next or previous first.");
}
public void displayList() {
System.out.print("List: ");
for (String entry : studentMarks) System.out.print(entry + " → ");
System.out.println("null");
}
}
Key Points:
ListIteratorallows bi-directional traversal (next(),previous()).set()andremove()only work after anext()orprevious().- Use
add()to insert elements at the current cursor position. - Perfect for managing lists where elements are frequently updated.
Q2. TaskManager – Using LinkedList
Objective: Maintain a task list with add, remove, update, and display operations.
import java.util.*;
class TaskManager {
private LinkedList<String> tasks = new LinkedList<>();
public void addTask(String task) {
tasks.add(task);
System.out.println("Task added: " + task);
}
public void removeTask(String task) {
if (tasks.remove(task)) System.out.println("Task removed: " + task);
else System.out.println("Task not found: " + task);
}
public void updateTask(String oldTask, String newTask) {
int index = tasks.indexOf(oldTask);
if (index != -1) {
tasks.set(index, newTask);
System.out.println("Task updated: " + oldTask + " → " + newTask);
} else System.out.println("Task not found: " + oldTask);
}
public void displayTasks() {
System.out.print("Tasks: ");
for (String task : tasks) System.out.print(task + " → ");
System.out.println("null");
}
}
Key Points:
LinkedListis ideal for frequent insertions and deletions.indexOf()helps find elements for updating.- Traversal and display can be done with a for-each loop.
- LinkedList maintains order and allows efficient head/tail operations.
Week 8 Concepts Summary
-
ListIterator -
Traversal:
next(),previous() - Modification:
add(),set(),remove() - Track current position with boolean flags like
canRemoveOrSet. -
LinkedList -
Dynamic data structure for tasks or items.
- Supports
add(),remove(),set(), andindexOf(). -
Dynamic List Management
-
Both
ArrayListandLinkedListsupport dynamic size. ArrayListis better for random access,LinkedListfor frequent insertion/deletion.- User-driven menus with
Scannerand loops allow interactive manipulation.