JDK-6349852 : Support java.util.Enumeration in enhanced for loop
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux
  • CPU: x86
  • Submitted: 2005-11-14
  • Updated: 2010-04-04
  • Resolved: 2006-11-02
Related Reports
Relates :  
Relates :  
Description
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 ----------

Comments
EVALUATION This is practically a duplicate of 6312085, which requests Iterators as valid Expressions in the enhanced-for loop. The Evaluation there applies here. (The description says 'but the enhanced for ("foreach") loop only accepts java.util.Iterator'...it means Iterable.)
02-11-2006