简体   繁体   中英

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

says:

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. 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 :

  • It's an optional operation - not all things that provide an Iterator need to implement remove()
  • It can only be called once for every call to next()
  • It's not reliable if the underlying collection is modified during enumeration other than by calling remove()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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