Java Reference
In-Depth Information
}
current = start;
}
}
// Add an item object to the list
public void addItem(Object item) {
ListItem newEnd = new ListItem(item); // Create a new ListItem
if(start == null) { // Is the list empty?
start = end = newEnd; // Yes, so new element is
start and end
} else { // No, so append new
element
end.next = newEnd; // Set next variable for
old end
end = newEnd; // Store new item as end
}
}
// Get the first object in the list
public Object getFirst() {
current = start;
return start == null ? null : start.item;
}
// Get the next object in the list
public Object getNext() {
if(current != null) {
current = current.next; // Get the reference to the
next item
}
return current == null ? null : current.item;
}
private ListItem start = null; // First ListItem in the list
private ListItem end = null; // Last ListItem in the list
private ListItem current = null; // The current item for
iterating
private class ListItem {
// Constructor
public ListItem(Object item) {
this.item = item; // Store the item
next = null; // Set next as end point
}
// Return class name & object
@Override
public String toString() {
return "ListItem " + item ;
}
ListItem next; // Refers to next item in
Search WWH ::




Custom Search