title: Using ListIterator to move back and forth over a LinkedList in Java
body: <p>I have a LinkedList over which I need to iterate back and forth multiple times. I am using it to keep track of a series of pages in a workflow that will be created dynamically.  This does not behave as I would expect.  Given this example:</p>
  
  <pre><code>LinkedList&lt;String&gt; navigationCases;
  navigationCases.add("page1");
  navigationCases.add("page2");
  navigationCases.add("page3");
  navigationCases.add("page4");
  
  ListIterator navigationItr = navigationCases.listIterator();
  navigationItr.next(); // Returns page1
  navigationItr.next(); // Returns page2
  navigationItr.previous(); //Returns page2 again
  navigationItr.next(); //Returns page2 again
  </code></pre>
  
  <p>I thought perhaps I was building my list incorrectly, or using the Iterator wrong, but after reading the documentation, this seems to be by design:</p>
  
  <p><strong>A ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().</strong></p>
  
  <p>And:</p>
  
  <p><strong>(Next) Returns the next element in the list. This method may be called repeatedly to iterate through the list, or intermixed with calls to previous to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)</strong></p>
  
  <p>So after reading this, it is clear why my code is behaving the way it does.  I just don't understand why it should work this way.  Even remove seems to be bending over backwards to accommodate this implementation:</p>
  
  <p><strong>Note that the remove() and set(Object) methods are not defined in terms of the cursor position; they are defined to operate on the last element returned by a call to next() or previous().</strong></p>
  
  <p>Conceptually, a LinkedList seemed to model my workflow cases pretty well, but I can't use an Iterator that behaves this way.  Am I missing something here, or should I just write my own class maintain a list of cases and navigate through them?</p>