用于替换 Java 中列表成员的迭代器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/853549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:04:34  来源:igfitidea点击:

iterator for replacing list members in Java?

javalistiterator

提问by Jason S

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

嗯... JavaIterator<T>remove()方法但没有replace(T replacement)方法。

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 很好,但对于链表会很糟糕。

回答by Tom Hawtin - tackline

ListIterator.setas returned by List.listIterator()or List.listIterator(int)

ListIterator.setList.listIterator()或返回List.listIterator(int)

(setwouldn't make any sense for, say, a Setiterator.)

set对于Set迭代器来说没有任何意义。)

回答by Michael Myers

You need a ListIteratorinstead of an Iterator(listIterator()gives you one). Then use the setmethod.

你需要一个ListIterator而不是一个IteratorlistIterator()给你一个)。然后使用该set方法。