title: Why can't I use foreach on my Class?
body: <p>I'm trying to use my custom class with an iterator, but it can't iterate on the elements using <code>foreach</code>. How can I deal with it?</p>
  
  <pre><code>public class FCSOfOtherClass&lt;Double&gt; {
  private int n;
  private Double[] a;
  
  public FCSOfOtherClass(int cap) {
      a = (Double[]) new Object[cap];
  }
  
  public void push(Double dou) {
      if (a.length == n) {
          this.resize(2 * a.length);
          a[n++] = dou;
      } else {
          a[n++] = dou;
      }
  }
  
  private void resize(int max) {
      Double[] newa = (Double[]) new Object[max];
      for (int i = 0; i &lt; n; i++) {
          newa[i] = a[i];
          a = newa;
      }
  }
  
  public Boolean isEmpty() {
      return n == 0;
  }
  
  public Double pop() {
      Double dou = a[--n];
      a[n] = null;
      if (n &gt; 0 &amp;&amp; n == a.length / 2) {
          resize(a.length / 2);
      }
      return dou;
  }
  
  public int size() {
      return n;
  }
  
  public Iterator&lt;Double&gt; iterator() {
      return new RAIterator();
  }
  
  private class RAIterator implements Iterator&lt;Double&gt; {
      private int i = n;
  
      @Override
      public boolean hasNext() {
          return i &gt; 0;
      }
  
      @Override
      public Double next() {
          return a[--i];
      }
  
      @Override
      public void remove() {
  
      }
  
      @Override
      public void forEachRemaining(Consumer&lt;? super Double&gt; action) {
  
      }
  </code></pre>
  
  <p>This is my main method:</p>
  
  <pre><code>public static void main(String[] args) {
      FCSOfOtherClass&lt;Integer&gt; fcs = new FCSOfOtherClass&lt;&gt;(100);
      int i = 0;
      while (!StdIn.isEmpty()) {
          fcs.push(++i);
      }
      for (int j:fcs) {
          StdOut.print(j);
      }
  }
  </code></pre>
  
  <p>When I run this, I get an error telling me that <code>foreach</code> is not applicable to my type.</p>