Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: overhauled code samples

...

This noncompliant code example implements a BankOperations class  class with a removeAccounts() method used to terminate all the accounts of a particular account holder, as identified by the name. Names can be repeated in the vector if a person has more than one account. The remove() method attempts to iterate through all the vector entries comparing each entry with the name "Harry."

Code Block
bgColor#FFcccc
class BankOperations {
  private static void removeAccountsremoveAccount(VectorVector<String> v, String name) {
    Enumeration e = v.elements();
		          
    while (e.hasMoreElements()) {
      String s = (String) e.nextElement();
      if (s.equals(name)) {
        v.remove(name); // Second Harry is not removed
      }
    }

    // Display current account holders
    System.out.println("The names are:");  }
    e = v.elements();
    while (e.hasMoreElements()) {
      System.out.println(e.nextElement());  // Prints Dick, Harry, Tom	  
    }
  }
	 
  public static void main(String args[]) { 
    // List contains a sorted array of account holder names
    // Repeats are admissible 
    ListList<String> list = new ArrayListArrayList<String>( Arrays.asList(
      new String[] {"Dick", "Harry", "Harry", "Tom"}));
    VectorVector<String> v = new VectorVector<String>(list);
    removeAccount(v, "Harry"); 
    // Display current account holders
    System.out.println("The names are:");
    Enumeration e = v.elements();
    while (e.hasMoreElements()) {
      System.out.println(e.nextElement());  // Prints Dick, Harry, Tom    
    }
  }
}

Upon encountering the first "Harry," it successfully removes the entry, and the size of the vector diminishes to three. However, the index of the Enumeration remains unchanged, causing the program to perform the next (now final) comparison with "Tom". Consequently, the second "Harry" remains in the vector unscathed, having shifted to the second position in the vector.

...

This compliant solution remedies the problem described in the noncompliant code example and demonstrates the advantages of using an Iterator over an Enumeration:. It also uses Java's for-each syntax to iterate over the Vector implicitly. This is a more concise way of iterating over any collection, but is not useful when the iterator must be accessed directly (such as when removing an element).

Code Block
bgColor#ccccff
class BankOperations {
  private static void removeAccountsremoveAccount(VectorVector<String> 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:");
  public static i = v.iterator();
    while (i.hasNext()void main(String args[]) { 
      System.out.println(i.next()); // PrintsList Dick,contains Toma only	sorted 
array of account holder }names
  }
	 
 // publicRepeats static void main(String args[]) {are admissible 
    ListList<String> list = new ArrayListArrayList<String>( Arrays.asList(
      new String[] {"Dick", "Harry", "Harry", "Tom"}));
    VectorVector<String> v = new VectorVector<String>(list);
    removeremoveAccount(v, "Harry"); 
    // Display current account holders
    System.out.println("The names are:");
    for (String s: v) {
      System.out.println(s); // Prints Dick, Tom only     
    }
  }
}

Applicability

Using Enumeration when performing remove operations on an iterable Collection may cause unexpected program behavior.

...