[Solved] ListIterator Strange Adding to ArrayList


Your question isn’t clear, but I suspect the main point you may be missing is that ListIterator.add inserts at the current location:

The element is inserted immediately before the next element that would be returned by next(), if any, and after the next element that would be returned by previous(), if any.

And also:

The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected

So after your initial call to it.add("##") the list contains "##", "is" "us". You’re then moving next twice – the first moves the cursor to just after "is" (which is returned). The second moves the cursor to just after "us" (which is returned). Then the call to previous() returns "us" again, and finally a call to set() replaces "us" with "##":

Replaces the last element returned by next() or previous() with the specified element (optional operation).

It all looks like it’s obeying the documentation perfectly. Unfortunately it’s not clear which of these steps is confusing you, as you’ve conflated so many in one question.

1

solved ListIterator Strange Adding to ArrayList