title: Implementing the Java Iterator and Iterable Interface over an Object[] array
body: <p>Here is my internal class that implements Iterator:</p>
  
  <pre><code>public class myIterator implements Iterator&lt;Object&gt; {
      private int pos = 0;
  
      public boolean hasNext(){
          return pos &lt; arrayPal.length;
  
      }
      public Object next(){
          return arrayPal[pos++];
  
      }
  }
  </code></pre>
  
  <p>In the class that this internal class is within, DynArray, (which is an object that has an internal array called arrayPal of any type of object, as well as a boolean value that says whether or not the array can store null values) I want to use the iterator for my add method </p>
  
  <pre><code>public void add( T ele ) {
  
      int counter =0;
      int tempIndex =0;
      if(ele == null &amp;&amp; this.allowed == false){
          throw new NullPointerException("This array does not accept null values!");
      }
  
      else{
          myIterator iterator = new myIterator();
          Iterator&lt;T&gt; it = this.iterator();
  
  
          for(Object o: this.arrayPal){
              System.out.print(it.hasNext());
              if(it.hasNext() == false &amp;&amp; (this.arrayPal.length-1) &gt; counter){
                  arrayPal[counter+1] = ele;
              }
              else if (it.hasNext() == false &amp;&amp; (arrayPal.length-1) == counter){
                  //expand the array by quanta
                  //add the the first newest index
                  Object[] temp = new Object[this.arrayPal.length + quanta];
  
                  for(Object b: this.arrayPal){
                      temp[tempIndex++] = b;
                  }
                  temp[this.arrayPal.length] = ele;
                  this.arrayPal = temp;
  
              }
          counter++;
          }
  }
  </code></pre>
  
  <p>However the .hasNext() keeps returning true even for the last element in my list. Why is this? </p>
  
  <p>Additionally, it will not allow me to do this.arrayPal.iterator() it says:</p>
  
  <p>Cannot invoke iterator() on the array type 
   Object[]</p>
  
  <p>Why not?</p>