Java Reference
In-Depth Information
{
class Iter implements Iterator
{
int index = 0;
@Override
public boolean hasMoreElements()
{
return index < toDoList.length;
}
@Override
public Object nextElement()
{
return toDoList[index++];
}
}
return new Iter();
}
void add(ToDo item)
{
toDoList[index++] = item;
}
}
Listing3-15 demonstrates Iterator ,therefactored ToDoList class,and Listing
3-7 ' s ToDo class.
Listing 3-15. Creating and iterating over a ToDoList of ToDo instances with a reusable iterat-
or
class LCDemo
{
public static void main(String[] args)
{
ToDoList toDoList = new ToDoList(5);
toDoList.add(new ToDo("#1", "Do laundry."));
toDoList.add(new ToDo("#2", "Buy groceries."));
toDoList.add(new ToDo("#3", "Vacuum apartment."));
Search WWH ::




Custom Search