Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It doesn't make sense for <code>Enumeration</code> to implement <code>Iterable</code>. <code>Iterable</code> is a factory method for <code>Iterator</code>. <code>Enumeration</code> is analogous to <code>Iterator</code>, and only maintains state for a single enumeration.</p> <p>So, be careful trying to wrap an <code>Enumeration</code> as an <code>Iterable</code>. If someone passes me an <code>Iterable</code>, I will assume that I can call <code>iterator()</code> on it repeatedly, creating as many <code>Iterator</code> instances as I want, and iterating independently on each. A wrapped <code>Enumeration</code> will not fulfill this contract; don't let your wrapped <code>Enumeration</code> escape from your own code. (As an aside, I noticed that Java 7's <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/DirectoryStream.html" rel="noreferrer"><code>DirectoryStream</code></a> violates expectations in just this way, and shouldn't be allowed to "escape" either.)</p> <p><code>Enumeration</code> is like an <code>Iterator</code>, not an <code>Iterable</code>. A <code>Collection</code> is <code>Iterable</code>. An <code>Iterator</code> is not.</p> <p>You can't do this:</p> <pre><code>Vector&lt;X&gt; list = … Iterator&lt;X&gt; i = list.iterator(); for (X x : i) { x.doStuff(); } </code></pre> <p>So it wouldn't make sense to do this:</p> <pre><code>Vector&lt;X&gt; list = … Enumeration&lt;X&gt; i = list.enumeration(); for (X x : i) { x.doStuff(); } </code></pre> <p>There is no <code>Enumerable</code> equivalent to <code>Iterable</code>. It could be added without breaking anything to work in for loops, but what would be the point? If you are able to implement this new <code>Enumerable</code> interface, why not just implement <code>Iterable</code> instead?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload