Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the problem you have comes from <code>remove(i)</code>.</p> <p>When you remove the <code>i-th</code> element, the <code>i+1-th</code> element becomes the <code>i-th</code> and so on: every element is shifted. Therefore if you need to remove 2 elements in your list that are at index <code>j</code> and <code>j+1</code>, removing the <code>j-th</code> element calling <code>remove(j)</code> will shift the <code>j+1-th</code> element at the index <code>j</code>. Hence removing that second element requires calling <code>remove(j)</code> again, and not <code>remove(j+1)</code>.</p> <p>So you need to decrement <code>i</code> after removing.</p> <p>Since your <code>remove</code> method actually decrements <code>numberOfEntries</code>, the condition on your <code>while</code> loop is properly updated. So all you need to do is replace</p> <pre><code>if (item.equals(currentNode.getData())) { index++; remove(i); } else { currentNode = currentNode.getNextNode(); } </code></pre> <p>by</p> <pre><code>if (item.equals(currentNode.getData())) { index++; remove(i--); } // update the current node, whether removing it or not currentNode = currentNode.getNextNode(); </code></pre> <hr> <p><strong>Iterator.remove()</strong></p> <p>This problem you are describing shows the usefulness of <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html#remove%28%29" rel="nofollow">Iterator.remove()</a> when using data structures from the JDK for going through an iterable collection and removing elements as you go through it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload