<p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;">ListIterator接口继承自Iterator接口,新增了add()等方法。
<p style="color:rgb(51,51);font-family:Arial;font-size:14px;">关于ListIterator的add()方法的作用(接口是没有方法实现的,但其实现类对于add()方法的实现机制大致相同,姑且这样说吧),《<a href="http://lib.csdn.net/base/javase" rel="nofollow" class="replace_word" title="Java SE知识库" style="color:rgb(223,52,52);text-decoration:none;font-weight:bold;">Java核心技术
卷I》里如下表述:
<p style="color:rgb(51,51);font-family:Arial;font-size:14px;"> <span style="color:rgb(255,102,102);"> “如果多次调用add方法,将按照提供的次序把元素添加到链表中。它们被依次添加到迭代器当前位置之前。”
<p style="color:rgb(51,51);font-family:Arial;font-size:14px;">对于这种说法,很容易引发歧义,当前位置是什么?当前指向的元素,还是游标位置?
<p style="color:rgb(51,51);font-family:Arial;font-size:14px;">带着这种疑问,我查阅了ListIterator接口的API说明文档(网址见本文结尾),得到对于add()方法的如下英文描述:
<p style="color:rgb(51,51);font-family:Arial;font-size:14px;"><span style="color:rgb(255,102);">Inserts the specified element into the list (optional operation). The element is inserted immediately before the element that would be returned by
<a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/ListIterator.java#ListIterator.next%28%29" rel="nofollow" title="java.util.ListIterator.next()" style="color:rgb(51,153);text-decoration:none;">next()
,if any,and after the element that would be returned by <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/ListIterator.java#ListIterator.previous%28%29" rel="nofollow" title="java.util.ListIterator.previous()" style="color:rgb(51,153);text-decoration:none;">previous()
,if any. (If the list contains no elements,the new element becomes the sole element on the list.)<p style="color:rgb(51,51);font-family:Arial;font-size:14px;">该描述就很清晰:的确是把新元素插入由<span style="color:rgb(255,102);">next()所返回的那个元素之前,<span style="color:rgb(255,102);">prevIoUs()所返回的元素之后。之所以加之前与之后两个限定,是为了应对在链尾(next返回为空)以及链头(prevIoUs返回为空)的特殊情况,如果链表为空,则新插入的元素就作为该链表的唯一元素。另外,每当插入一个元素以后,迭代器都会后移(向着链尾方向)一位。
<p style="color:rgb(51,51);font-family:Arial;font-size:14px;">举例说明:
<div class="dp-Highlighter bg_java" style="font-family:Consolas,'Courier New',Courier,mono,serif;color:rgb(51,51);">
<div class="bar">
<div class="tools" style="font-size:9px;line-height:normal;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;color:#C0C0C0;border-left-width:3px;border-left-style:solid;border-left-color:rgb(108,226,108);">
[java] <a href="http://blog.csdn.net/goodbaby728/article/details/10163347#" rel="nofollow" class="ViewSource" title="view plain" style="color:rgb(160,160,160);text-decoration:none;border:none;display:inline-block;width:16px;text-indent:-2000px;">view
plain <a href="http://blog.csdn.net/goodbaby728/article/details/10163347#" rel="nofollow" class="CopyToClipboard" title="copy" style="color:rgb(160,160);text-decoration:none;border:none;display:inline-block;width:16px;text-indent:-2000px;">copy
<div style="width:18px;z-index:99;">