tags:

views:

1906

answers:

5

Why can't I do:

Enumeration e = ...
for (Object o : e)
  ...
+15  A: 

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.

cletus
Next question: why aren't Enumerations Iterable? http://stackoverflow.com/questions/27240/why-arent-enumerations-iterable
Michael Myers
Is there a semantic difference between the two? (I'm not familiar with the Iterable class)? Why not have for work on Enumeration as well?
ripper234
Because Enumerations are more akin to Iterator, not Iterable (see the stackoverflow question mmyers posted). As a workaround, you can do `for(; e.hasMoreElements(); ) { e.getNextElement(); }`
Tim
But `Iterators` are not `Iterable` either. You can't do `for (obj : itr)`. The reason for this is that `Iterable` is really *repeatedly iterable*, whereas an iterator can only be iterated over once.
oxbow_lakes
Do you mean "Enumeration<T> doesnt Implement Iterably<T>" instead of extends?
James.Elsey
A: 

The new-style-for-loop ("foreach") works on arrays, and things that implement the Iterable interface.

It's also more analogous to Iterator than to Iterable, so it wouldn't make sense for Enumeration to work with foreach unless Iterator did too (and it doesn't). Enumeration is also discouraged in favor of Iterator.

Laurence Gonsalves
Let's be careful with terms here. Deprecation means something very specific when discussing APIs. Use of Enumeration is discouraged, but it's _not_ deprecated.
ykaganovich
ykaganovich: good point. I've corrected the text in the answer.
Laurence Gonsalves
A: 

Because an Enumeration (and most classes derived from this interface) does not implement Iterable.

You can try to write your own wrapper class.

Stroboskop
You shouldn't write a wrapper for Enumeration that turns it into an Iterable any more than you should create such a wrapper for Iterator. Iterator and Enumeration objects are stateful with respect to iteration. Iterables are not.
Laurence Gonsalves
A: 

"Let's be careful with terms here. Deprecation means something very specific when discussing APIs. Use of Enumeration is discouraged, but it's not deprecated." It should be. :)

Faldegast
+3  A: 

You could use Collections#list(Enumeration) to iterate over them that way anyway.

Enumeration e = ...
for (Object o : Collections.list(e))
  ...
BalusC