简体   繁体   English

在java的所有情况下iterator.remove()是否都是健全的?

[英]Is iterator.remove() is ROBUST in all cases in java?

与在C ++中一样,iterator.remove()不是100%安全或健壮,java是否保证iterator.remove()具有100%的健壮性?

Going off of matt's comment (who really deserves the credit) 关于亚马的评论(谁真的值得信任)

http://java.sun.com/javase/6/docs/api/java/util/Iterator.html#remove%28%29 http://java.sun.com/javase/6/docs/api/java/util/Iterator.html#remove%28%29

says: 说:

void remove() void remove()

Removes from the underlying collection the last element returned by the iterator (optional operation). 从底层集合中移除迭代器返回的最后一个元素(可选操作)。 This method can be called only once per call to next. 每次调用next时,只能调用一次此方法。 The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method. 如果在迭代正在进行中以除调用此方法之外的任何方式修改基础集合,则未指定迭代器的行为。

So...... yes, there are conditions under which unexpected/unsafe behaviour can occur. 所以......是的,有条件可能会发生意外/不安全的行为。

Yes, it's robust in that it's defined on the interface and thus has to work on any collection where it's implemented. 是的,它很强大,因为它是在界面上定义的,因此必须在任何实现它的集合上工作。 However, there are several caveats that come from the Javadoc : 但是,有一些警告来自Javadoc

  • It's an optional operation - not all things that provide an Iterator need to implement remove() 这是一个可选操作 - 并非所有提供Iterator的东西都需要实现remove()
  • It can only be called once for every call to next() 它只能在每次调用next()调用next()
  • It's not reliable if the underlying collection is modified during enumeration other than by calling remove() 如果底层集合是通过调用比其他枚举过程中修改了它的不可靠remove()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM