title: How to return list iterator of the elements in a list
body: <p>This is something i don't understand, and my professor is busy this week so i can't ask her. Can someone explain to me how to set up a list iterator, and use it to return elements in an ArrayList? thanks. It is the the method "public Iterator iterator()", i've tried my best with it, but i can't complete it.</p>
  
  <pre><code>public class ArrayList&lt;E&gt; implements List&lt;E&gt; {
  private E[] elementData;
  private int elementCount;
  private int capacityIncrement;
  private static final int INVALID_INDEX=-1;
  private static final int DEFAULT_CAPACITY = 100;
  
  public ArrayList() {
  
      capacityIncrement = 0;
      elementData = (E[]) new Object[DEFAULT_CAPACITY];
  
  
  }
  
  public ArrayList(int capacity) {
  
      this.capacityIncrement = 0;
      this.elementData = (E[]) new Object[capacity];
  
  
  }
  
  public ArrayList(int capacity, int increment) {
  
      this.capacityIncrement = increment;
      this.elementData = (E[]) new Object[capacity];
  
  }
  
  public int size() {
      return elementCount;
  
  }
  
  public boolean isEmpty() {
      if (elementCount != 0) return false;
      else return true;
  }
  
  public void clear() {
          elementCount = 0;
  
  }
  
  public boolean contains(E element) { //check back
      for (int i = 0; i &lt; elementCount; i++) {
          if (elementData[i].equals(element)) return true; //== vs. .equals
      }
      return false;
  }
  
  public void add(E element) {
      elementCount++;
      elementData[elementCount] = element;
  }
  
  public boolean remove(E element) {
      for (int i = 0; i &lt; elementCount; i++) { //while vs. loop
          if (elementData[i].equals(element)) {
              for (int j = 0; j &lt;= (elementCount - i); j++){
              elementData[i] = elementData[i++];
              elementCount = elementCount - 1;
              return true;
              }
  
  
          }
  
      }
      return false;
  }
  
  public E elementAt(int index) {
  
      return elementData[index]; //elementdata vs. elementcount
  }
  
  public int indexOf(E element) {
  
  
      for (int i = 0; i &lt; elementCount; i++) //while vs. loop
          if (elementData[i].equals(element)) {
              return i;
  
          } return INVALID_INDEX;
  
  
  }
  
  public void insertElementAt(E element, int index) {
      elementCount = elementCount + 1;
      for (int i = index; i &lt; elementCount; i++) {
          elementData[i++] = elementData[i];
      }
      elementData[index] = element;
       //shift right (look at notes)
  }
  
  public void removeElementAt(int index) {
      for (int i = index; i &lt; elementCount; i++) {
          elementData[i] = elementData[i++];
  
      }
      elementCount = elementCount - 1;
  }
  
  public void setElementAt(E element, int index) {
  
      elementData[index] = element;
  }
  
  public void removeDuplicates() {
  
      for (int i = 0; i &lt; elementCount; i++) {
          for (int j = 0; j &lt; elementCount; j++) {
              if (elementData[i].equals(elementData[j])) {
                  elementData[i] = elementData[i++];
                  elementCount = elementCount - 1;
              }
          }
      }
  }
  
  public void trimToSize() { //don't need to add trims to removing methods??
  
  
  
  }
  
  public Iterator&lt;E&gt; iterator() {
       Iterator itr = new list.iterator();
       while (itr.hasNext()) {
  
       }
      /**
   *
   * @return  a list iterator of the elements
   * in this list (in proper sequence).
   */
  
  }
  
  public Iterator&lt;E&gt; iterator(int index) {
      throw new UnsupportedOperationException("Not supported yet.");
  }
  
  
  
  private static class ArrayListIterator&lt;E&gt; implements Iterator&lt;E&gt; {
      private ArrayListIterator(ArrayList c) {
        /**
        * Returns a list iterator of the elements in this list (in proper sequence).
        * @param c list to be iterated upon
        */
          elementData=c;
  
      }
  
  
   }
  
   }
  </code></pre>