[英]what is the need to have listIterator() and iterator() in the list interface in Java?
The List
interface has two methods listIterator()
and iterator()
. List
接口有两个方法listIterator()
和iterator()
。 why both of these are needed. 为什么这两个都是必需的。
Iterator<E> iterator()
Returns an iterator over the elements in this list in proper sequence.
ListIterator<E> listIterator()
Returns a list iterator over the elements in this list (in proper sequence).
ListIterator<E> listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the
specified position in the list. The specified index indicates the first element that would be
returned by an initial call to next. An initial call to previous would return the element with the
specified index minus one.
so basically, ListIterator()
has this additional methods to get previous and next
elements while Iterator()
has only next
elements. 所以基本上, ListIterator()
有另外的方法来获取previous and next
元素,而Iterator()
只有next
元素。 Is this only for this purpose, there is another ListIterator() interface
and listiterator() method in List inteface
这只是为了这个目的, listiterator() method in List inteface
有另一个listiterator() method in List inteface
ListIterator() interface
和listiterator() method in List inteface
Since Java 5 it is simply possible to override a method with a more specific return type (called covariant return type ). 从Java 5开始,可以简单地使用更具体的返回类型(称为协变返回类型 )来覆盖方法。 But ListIterator
has been introduced with Java 1.2. 但是ListIterator
已经引入了Java 1.2。 To avoid casts on usage of iterator()
there has to be a new method. 为了避免在使用iterator()
强制转换,必须有一个新方法。
The API could not have been changed from Java 5 on because that would have broken all implementations of List
which do not declare iterator()
returning ListIterator
also if most implementations return a ListIterator
instance in real. API无法从Java 5更改,因为如果大多数实现在实数中返回ListIterator
实例,那么就会破坏List
所有实现,这些实现也不会声明iterator()
返回ListIterator
。
A similar dilemma is Enumeration
and Iterator
. 类似的困境是Enumeration
和Iterator
。 Nowadays Iterator
would extend Enumeration
and simply add the remove()
method. 现在, Iterator
将扩展Enumeration
并简单地添加remove()
方法。 Or better Iterator
would have replaced Enumeration
and a ModifiableIterator
with an additional remove()
would have been added. 或者更好的Iterator
会替换Enumeration
并且会添加一个带有额外remove()
的ModifiableIterator
。
Specifically they are both required because List
is a type of Iterable
which specifies an Iterator iterator()
method. 具体来说,它们都是必需的,因为List
是一种Iterable
类型,它指定了Iterator iterator()
方法。
Now List
could have simply overridden the iterator()
method in Iterable
to declare a return type of ListIterator
which is type of Iterator
(thus satisfying the contract). 现在List
可以简单地覆盖了iterator()
的方法Iterable
声明的返回类型ListIterator
这类型的Iterator
(从而满足合同)。 Why this was not done is probably a design decision. 为什么没有这样做可能是一个设计决定。
EDIT: @ajb pointed out in a comment that covariant return types were added in Java SE 5 after the List
interface was created. 编辑:@ajb在评论中指出,在创建List
接口后,Java SE 5中添加了协变返回类型。 This explains why List
has both methods, as iterator()
return type could not be narrowed to ListIterator
in Java 1.2. 这解释了为什么List
有两种方法,因为iterator()
返回类型无法缩小到Java 1.2中的ListIterator
。
Java is designed to be very sturdy--alowing a LOT of language implementation changes that don't affect the code running on the java. Java的设计非常坚固 - 允许大量的语言实现更改,这些更改不会影响在java上运行的代码。
For instance, you can generally replace the "Java JRE 6" with "Java JRE 7" and not have a problem with most code--that's an entire language upgrade with new syntax and all. 例如,您通常可以将“Java JRE 6”替换为“Java JRE 7”,而不会出现大多数代码的问题 - 这是使用新语法的全部语言升级。 You can also replace the Java JRE with an open source implementation and you will have the same results--stuff will pretty much just work. 您也可以使用开源实现替换Java JRE,您将获得相同的结果 - 这些东西几乎可以正常工作。
So when they design a class, they have to design it for both the implementor and the coder. 因此,当他们设计一个类时,他们必须为实现者和编码器设计它。
If they were to simply have an ArrayList be "Iterable" then for ArrayLists return an Iterator of type ListIterator then another implementor (OpenJDK) would not be required by the interface to return the correct type--not only that but the user would have no idea what type was being returned without looking at the docs (Eclipse ctrl-space would be less helpful if everyone programmed this way). 如果他们只是让一个ArrayList为“Iterable”,那么对于ArrayLists返回一个ListIterator类型的Iterator,接口不需要另一个实现者(OpenJDK)来返回正确的类型 - 不仅如此,用户也没有想法在没有查看文档的情况下返回了什么类型(如果每个人都以这种方式编程,那么Eclipse ctrl-space将没那么有用)。
Java generally tries to be as explicit as physically possible... this is a Very Good Thing because it stops guessing and related errors. Java通常试图尽可能明确地表达......这是一件非常好的事情,因为它会停止猜测和相关的错误。
When it's not possible to make the interfaces and types explicit enough they are extremely deliberate with their documentation, but they avoid that if at all possible. 当不可能使接口和类型足够明确时,他们非常谨慎地使用他们的文档,但是如果可能的话他们会避免这种情况。
似乎Iterator将用于单链表,ListIterator用于双链表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.