加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 06:37:59 所属栏目:Java 来源:网络整理
导读:转自: 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()方法的作用(接口是没有方法实现

转自:

<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();??

  • ?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方法依赖于迭代器的状态。(红色是我的备注)

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读