Java Reference
In-Depth Information
toDoList.add(new ToDo("#4", "Write report."));
toDoList.add(new ToDo("#5", "Wash car."));
Iterator iter = toDoList.iterator();
while (iter.hasMoreElements())
System.out.println(iter.nextElement());
}
}
The Iterator instance that is returned from iterator() returns ToDo items
in the same order as when they were added to the list. Although you can only use the
returned Iterator object once, you can call iterator() whenever you need a
new Iterator object.Thiscapabilityisabigimprovementovertheone-shotiterator
presented in Listing 3-9 .
Interfaces Within Classes
Interfacescanbenestedwithinclasses.Oncedeclared,aninterfaceisconsideredtobe
static,evenifitisnotdeclared static .Forexample, Listing3-16 declaresanenclos-
ing class named X along with two nested static interfaces named A and B .
Listing 3-16. Declaring a pair of interfaces within a class
class X
{
interface A
{
}
static interface B
{
}
}
Youwouldaccess Listing3-16 'sinterfacesinthesameway.Forexample,youwould
specify class C implements X.A {} or class D implements X.B {} .
As with nested classes, nested interfaces help to implement top-level class architec-
ture by being implemented via nested classes. Collectively, these types are nested be-
causetheycannot(asin Listing3-14 's Iter localclass)orneednotappearatthesame
level as a top-level class and pollute its package namespace.
Search WWH ::




Custom Search