views:

549

answers:

2

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.

+8  A: 

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

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

Tom Hawtin - tackline
+7  A: 

You need a ListIterator instead of an Iterator (listIterator() gives you one). Then use the set method.

Michael Myers
+1 because I can only accept one of your answers
Jason S