Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6349852

Support java.util.Enumeration in enhanced for loop

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 5.0
    • specification
    • x86
    • linux

      A DESCRIPTION OF THE REQUEST :
      Many classes only return java.util.Enumeration to iterate over their values, but the enhanced for ("foreach") loop only accepts java.util.Iterator.




      JUSTIFICATION :
      Wrapping the Enumeration in an Iterator wrapper is possible, but is wasteful and awkward. Using an Enumeration in a standard for/while loop is tedious and awkward. It's inconvenient enough that we have two entirely different interfaces for iteration, but at least that could be glossed over if they could be used the same way.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      import java.util.*;
      import java.util.zip.*;
      public class ZipList {
          public static void main(String[] args) throws Exception {
              ZipFile zip = new ZipFile(args[0]);
              for (ZipEntry entry : zip.entries()) {
                  System.out.println(entry.toString());
              }
          }
      }

      ACTUAL -
      import java.util.*;
      import java.util.zip.*;
      public class ZipList {
          public static void main(String[] args) throws Exception {
              ZipFile zip = new ZipFile(args[0]);
              for (Enumeration<? extends ZipEntry> e = zip.entries(); e.hasMoreElements(); ) {
                  ZipEntry entry = e.nextElement();
                  System.out.println(entry.toString());
              }
          }
      }


      ---------- BEGIN SOURCE ----------
      import java.util.*;
      import java.util.zip.*;
      public class ZipList {
          public static void main(String[] args) throws Exception {
              ZipFile zip = new ZipFile(args[0]);
              for (ZipEntry entry : zip.entries()) {
                  System.out.println(entry.toString());
              }
          }
      }

      ---------- END SOURCE ----------

            abuckley Alex Buckley
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: