title: for-each loop can only iterate over an array or an instance of java.lang.Iterable
body: <p>The following code doesn't work. What's wrong with this code? Compiler complains in the for loop that <code>NumberList</code> isn't a <code>Iterable</code> class. </p>
  
  <p>What kind of class can be used in for-each loop? How to make <code>NumberList</code> iterable? I tried making <code>NumberList implement Iterable</code>, but it doesn't seem to work because I don't know how to define the Iterator properly.</p>
  
  <p>If someone could demonstrate how to make this code work, or link me to a tutorial that'd be great.</p>
  
  <pre><code>public class Test{
      public class NumberList{
          private int numItems;
          private Number[] numbers;
  
          public NumberList(int size){
              this.numbers = new Number[size];
              this.numItems=0;
          }
  
          public void add(Number n){
              this.numbers[this.numItems++]=n;
          }
      }
  
      public void printPairs() {
          ArrayList&lt;Integer&gt; num=new ArrayList&lt;Integer&gt;();
  
          NumberList numbers = new NumberList(50);
          numbers.add(4);
          numbers.add(5);
          numbers.add(6);
  
          for(Number n1: numbers){
              System.out.println(n1);
          }
      }
  }
  </code></pre>