Hmmm... the Java Iterator<T>
has a remove()
method but not a replace(T replacement)
method.
嗯... Java Iterator
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.
是否有一种有效的方法来替换列表中的选定项目?我可以使用for循环来调用get(i)和set(i)这对于ArrayList来说很好,但是会吸引链表。
2 个解决方案
#1
55
ListIterator.set
as returned by List.listIterator()
or List.listIterator(int)
List.listIterator()或List.listIterator(int)返回的ListIterator.set
(set
wouldn't make any sense for, say, a Set
iterator.)
(set,对Set Set迭代器来说没有任何意义。)
#2
30
You need a ListIterator
instead of an Iterator
(listIterator()
gives you one). Then use the set
method.
你需要一个ListIterator而不是Iterator(listIterator()给你一个)。然后使用set方法。
#1
55
ListIterator.set
as returned by List.listIterator()
or List.listIterator(int)
List.listIterator()或List.listIterator(int)返回的ListIterator.set
(set
wouldn't make any sense for, say, a Set
iterator.)
(set,对Set Set迭代器来说没有任何意义。)
#2
30
You need a ListIterator
instead of an Iterator
(listIterator()
gives you one). Then use the set
method.
你需要一个ListIterator而不是Iterator(listIterator()给你一个)。然后使用set方法。