Search code examples
javalistiterator

iterator for replacing list members in Java?


Hmmm... the Java Iterator<T> has a remove() method but not a replace(T replacement) method.

Is there an efficient way to replace selected items in a List? I can use a for-loop to call get(i) and set(i) which is fine for ArrayList, but would suck for a linked list.


Solution

  • ListIterator.set as returned by List.listIterator() or List.listIterator(int)

    (set wouldn't make any sense for, say, a Set iterator.)