Why can’t I use foreach on Java Enumeration?

Because Enumeration<T> doesn’t extend Iterable<T>. Here is an example of making Iterable Enumerations.

As to why that’s an interesting question. This isn’t exactly your question but it sheds some light on it. From the Java Collections API Design FAQ:

Why doesn’t Iterator extend Enumeration?

We view the method names for
Enumeration as unfortunate. They’re
very long, and very frequently used.
Given that we were adding a method and
creating a whole new framework, we
felt that it would be foolish not to
take advantage of the opportunity to
improve the names. Of course we could
support the new and old names in
Iterator, but it doesn’t seem
worthwhile.

That basically suggests to me that Sun wants to distance themselves from Enumeration, which is very early Java with quite a verbose syntax.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)