有关ListIterator接口的add与remove方法探究

前端之家收集整理的这篇文章主要介绍了有关ListIterator接口的add与remove方法探究前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转自:

<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;">

     name=();  
  1.  iter = name.listIterator();  

代码执行完毕,name链表内容如下1A2B3C4(数字只作为占位符,可以忽略,内容为ABC,从链头到链尾)

IoUs(),返回A,因此应把D插入AB之间,迭代器顺移到DB之间;依次类推,可以插入E。最终输出结果为:

Highlighter bg_java" style="font-family:Consolas,108);list-style:outside;color:inherit;line-height:18px;">
  • 方法,关于remove方法,API文档描述如下:

     or (optional operation). This call can only be made once per call to next orprevIoUs. It can be made only if has not been called after the last call to next or previous.

    ,而remove方法依赖于迭代器的状态。(红色是我的备注)

    API文档,推荐GrepCode,网址如下:

    nofollow" style="color:rgb(51,153);text-decoration:none;">http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/ListIterator.java?av=h#ListIterator

    猜你在找的Java相关文章