Java Reference
In-Depth Information
Click here to view code image
class BankOperations {
private static void removeAccounts(Vector v, String name) {
Iterator i = v.iterator();
while (i.hasNext()) {
String s = (String) i.next();
if (s.equals(name)) {
i.remove(); // Correctly removes all instances
// of the name Harry
}
}
// Display current account holders
System.out.println("The names are:");
i = v.iterator();
while (i.hasNext()) {
System.out.println(i.next()); // Prints Dick, Tom only
}
}
public static void main(String args[]) {
List list = new ArrayList(Arrays.asList(
new String[] {"Dick", "Harry", "Harry", "Tom"}));
Vector v = new Vector(list);
remove(v, "Harry");
}
}
Applicability
Using Enumeration when performing remove operations on an iterable Collection may
cause unexpected program behavior.
Bibliography
[API 2013]
Interface Enumeration<E>
Interface Iterator<E>
[Daconta 2003]
Item 21, “Use Iteration over Enumeration”
Search WWH ::




Custom Search