title: Implement Java Iterator and Iterable in same class?
body: <p>I am trying to understand Java <code>Iterator</code> and <code>Iterable</code> interfaces</p>
  
  <p>I am writing this class </p>
  
  <pre><code>class MyClass implements Iterable&lt;String&gt; {
      public String[] a = null;
      public MyClass(String[] arr) {
          a = arr;    
      }
  
      public MyClassIterator iterator() {
          return new MyClassIterator(this);
      }
  
      public class MyClassIterator implements Iterator&lt;String&gt; {
          private MyClass myclass = null;
          private int count = 0;
          public MyClassIterator(MyClass m) {
              myclass = m;    
          }
  
          public boolean hasNext() {
              return count &lt; myclass.a.length;
          }
          public String next() {
              int t = count;
              count++;
              return myclass.a[t];
          }
  
          public void remove() {
              throw new UnsupportedOperationException();
          }
      }   
  }
  </code></pre>
  
  <p>It seems to be working.</p>
  
  <p>Should I have:</p>
  
  <pre><code>Myclass implements Iterable&lt;Stirng&gt;, Iterator&lt;String&gt; {
  
  }
  </code></pre>
  
  <p>Or I should put <code>MyClassIterator</code> outside <code>MyClass</code> as</p>
  
  <pre><code>class MyClass implements Iterable&lt;String&gt; {
      public String[] a = null;
      public MyClass(String[] arr) {
          a = arr;    
      }
      public MyClassIterator iterator() {
          return new MyClassIterator(this);
      }
  }
  
  
      public class MyClassIterator implements Iterator&lt;String&gt; {
          private MyClass myclass = null;
          private int count = 0;
          public MyClassIterator(MyClass m) {
              myclass = m;    
          }
  
          public boolean hasNext() {
              return count &lt; myclass.a.length;
          }
          public String next() {
              int t = count;
              count++;
              return myclass.a[t];
          }
  
          public void remove() {
              throw new UnsupportedOperationException();
          }
      }   
  </code></pre>
  
  <p>Which one is better?</p>