列表迭代器支持移除操作吗?

huangapple go评论96阅读模式
英文:

Do the List iterators support remove?

问题

删除方法在Iterator和ListIterator中是可选的。您从JDK列表中获取的迭代器或列表迭代器是否实现了remove操作?文档中没有说明。

英文:

The remove method is optional in Iterator and ListIterator. Do the Iterators or ListIterators you get from the JDK lists implement the remove operation? This is not stated in the documentation.

答案1

得分: 3

总结:

  • ArrayList.iterator 支持 remove,尽管没有明确记录
  • LinkedList.iterator 支持 remove,尽管没有明确记录
  • CopyOnWriteArrayList.iterator 支持 remove
  • Collections.unmodifiableList 返回的 List 支持 remove

标准类的经验法则似乎是:除非有充分理由不支持,否则迭代器支持删除。

英文:

In summary:

  • ArrayList.iterator supports remove even though it's not explicitly documented
  • LinkedList.iterator supports remove even though it's not explicitly documented
  • CopyOnWriteArrayList.iterator does not support remove
  • List returned by Collections.unmodifiableList does not support remove

The rule of thumb for the standard classes seems to be: iterators support removal unless there's a good reason not to.

答案2

得分: 2

事实上,回答起来很有趣。Java在**迭代器(Iterators)**方面有着一段历史。让我来告诉你这段历史,然后你就会明白为什么事情会变成这样。

迭代器之前,Java有**枚举(Enumeration)**接口。枚举提供了以下方法:

  1. boolean hasMoreElements()
  2. Object nextElement()

如今,Java的旧类(例如Vector)支持枚举。

但随后,在Java开始以全新的方式编写其集合(Collection)框架——即为了使其集合框架现代化——枚举在Java中被淘汰了。现在,在新的集合(Collections)中,Java使用了迭代器。这些迭代器支持以下方法:

  1. boolean hasNext()
  2. E next()
  3. default void remove()
  4. default void forEachRemaining(Consumer<? super E> action)

我想我已经表达得很清楚了。

现在,考虑Java完全开始使用迭代器,但仍然存在大量使用枚举的遗留代码。那么问题来了,随着Java的更新,这些代码会发生什么变化呢?

因此,Java为你提供了一个机会,可以在一些遗留类上默认使用迭代器而不是枚举,而这些遗留类的迭代器只是枚举接口的包装器。但这些迭代器不支持remove(),因为这些类底层的枚举接口不支持remove()。所以,当你在这些迭代器上调用remove()时,会抛出UnsupportedOperationException异常。

我希望到这一点,一切都清楚了,你也得到了你的答案。但是让我再多说一点。

那些已经使用枚举编写的遗留类怎么办呢?

你可以像Java自己做的那样解决这个问题,只需为你正在编写的新代码提供迭代器包装器,覆盖枚举接口。通过这种方式,你将在新的Java特性中编写新代码,而无需更改旧代码 列表迭代器支持移除操作吗?

英文:

Actually, it's kind of interesting to answer. Java has a history behind Iterators. Let me tell you the history, Then, it'll be clear why things are like this.

Before Iterators java has Enumeration Interface. The methods Enumeration provided are:

  1. boolean hasMoreElements()
  2. Object nextElement()

Now, java legacy classes like Vector supports Enumeration.

But then, Enumeration got banned in Java while Java started writing its Collection framework in a totally new way i.e. to modernize its Collection framework. Now, in the new Collections java used Iterators. These iterators support the following mehtods:

  1. boolean hasNext()
  2. E next()
  3. default void remove()
  4. default void forEachRemaining(Consumer<? super E> action)

I think I've made myself clear to this point.

Now, think about java totally started using Iterators, but there is still a lot of legacy code out there with Enumerations. So, the question is what will happen with those code, as java got updated?

So, java has provided you with a opportunity to use Iterators over Enumerations by default with some legacy classes and those Iterators of those legacy classes are just a wrapper around Enumeration Interface. But these Iterators does not support remove() cause those classes underline Enumeration Interface does not support remove(), that's why. So, when you call remove() on those Iterators, it throws UnsupportedOperationException -> this exception.

I hope till this everything is clear and you've got ur answer. But let me allow to say a little more.

What about those legacy classes those already have been written using Enumerations?

You can solve this problem as java has already done for itself, just provide Iterator wrapper over the Enumeration Interface for new codes you are writing. In this way, you will be writing new codes in new java features and you don't have to change the old code too 列表迭代器支持移除操作吗?

答案3

得分: 1

文档确实说明了Iterator的默认实现不支持remove操作1。关于List#iterator是否支持remove操作取决于List的实现。例如,ArrayList 实现中定义的ListItr 支持remove操作。

英文:

The documentation does state that the default implementation of Iterator does not support remove. Whether or not the List#iterator supports remove depends on the List implementation. For example, ListItr defined in the ArrayList implementation supports remove.

huangapple
  • 本文由 发表于 2020年7月31日 21:19:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63192651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定