title: Java Iterator for primitive types
body: <p>I have a Java class of the following form:</p>
  
  <pre><code>class Example {
  
    private byte[][] data;
  
    public Example(int s) { data = new byte[s][s]; }
  
    public byte getter(int x, int y)         { return byte[x][y]; }
    public void setter(int x, int y, byte z) { byte[x][y] = z;    }
  }
  </code></pre>
  
  <p>I would like to be able to externally iterate over the private data using an iterator like so: </p>
  
  <p><code>for(byte b : Example) { ;/* do stuff */ }</code></p>
  
  <p>I tried to implement a private Iterator class but I ran into problems:</p>
  
  <pre><code>private class ExampleIterator implements Iterator {
    private int curr_x;
    private int curr_y;
  
    public ExampleIterator() { curr_x=0; curr_y=-1; }
    public boolean hasNext() { 
      return curr_x != field.length-1
          &amp;&amp; curr_y != field.length-1; //is not the last cell?
    }
    public byte next() { // &lt;-- Error is here: 
                         // Wants to change return type to Object
                         // Won't compile!
      if(curr_y=field.length) { ++curr_x; curr_y=0; }
      return field[curr_x][curr_y];
    }
    public void remove() { ; } //does nothing
  }
  </code></pre>
  
  <p>How would I implement an <em>external</em> iterator for primitive types (not generics)? Is this possible in Java?</p>