Java Reference
In-Depth Information
Three-part for loop
This is the standard
for
loop invented in the early 1970s in the C language. The syntax is:
for (init; test; change) {
// do something
}
Its most common form is with an
int
“index variable” or “loop variable”:
MyDataType[] data = ...
for (int i = 0; i < data.length; i++)
MyDataType d = data[i];
// do something with it
}
while loop
A
while
loop executes its loop body as long as (“while”) the test condition is true. Com-
monly used in conjunction with the
Enumeration
or
Iterator
, as in
Iterator<MyData> iterator = ...
while (iterator.hasNext()) {
MyData md = iterator.next();
//
}
Enumeration
methods have longer names—for example,
hasMoreElements()
and
nextElement()
.
Eschewing Duplicates with a Set
Problem
You want a structure that will avoid storing duplicates.