title: Java: Iterators
body: <p>So I'm working on a program that involves two datatypes: a linked list and a Arraylist. </p>
  
  <p>The linked List Iterator looks like: </p>
  
  <pre><code>private class NodeIterator implements Iterator&lt;StudentIF&gt; {
          private Node curr;
  
          public NodeIterator(Node head) {
              curr = head;
          }
  
          public void remove() { }
  
          public boolean hasNext() {
              if (curr == null)
                  return false;
              return true;
          }
  
          public StudentIF next() {
              Node temp = curr;
              curr = curr.getNext();
              return temp.getData();
          }
  
      } // end class NodeIterator
  </code></pre>
  
  <p>and I call the ArrayList Iterator method/class. </p>
  
  <pre><code>MyArrayListName.iterator();
  </code></pre>
  
  <p>Here's the method that does the work of calling the iterators: </p>
  
  <pre><code>public StudentIF getStudent(int id) {
      Iterator&lt;StudentIF&gt; xy = iterator();
      while (xy.hasNext()) {
          if (id == xy.next().getId()) {
              return xy.next();
          }
      }
      // Student doesn't exist
      return null;
  }
  </code></pre>
  
  <p>My problem is when I call my methods to get my object by their id(instance variable), it always grabs the NEXT object, not the object I want. How do I get the current object with both the Linked List and the Array list? </p>
  
  <p>Please help me! </p>