AbstractList的迭代器remove()方法疑问
2015-08-09 00:00
357 查看
public void remove() { if (lastRet == -1) throw new IllegalStateException(); checkForComodification(); try { AbstractList.this.remove(lastRet); if (lastRet < cursor)//这个判断的作用是? cursor--; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException e) { throw new ConcurrentModificationException(); } }
自己写了一个模拟类,测试发现不加这个判断也可以正常删除,不知道这判断的作用是什么。。。。。求高人指点
/** * 模仿LinkedList */ public class SimulateLinkedList { private Node first; private Node last; private int size; public int size(){ return this.size; } /** * 增加元素 * @param obj */ public void add (Object obj){ Node node = null; if(first==null){ node = new Node(null,obj,null); first = node; last = node; }else{ node = new Node(last,obj,null); last.next = node; last = node; } size++; } /** * 在指定位置增加元素 * @param index * @param obj */ public void add(int index,Object obj){ Node temp = node(index); if(temp!=null){ Node node = null; if(index > 0){ //非头结点 if(index !=size) {//非末尾结点 node = new Node(temp.previous,obj,temp); temp.previous.next = node; }else{//末尾结点 node = new Node(temp,obj,null); last.next = node; last = node; //更新末尾结点 } }else{//头结点 node = new Node(null,obj,temp); first = node;//更新链头结点 } temp.previous = node; size++; } } /** * 获取元素 * @param index * @throws Exception */ public Object get(int index) throws Exception{ rangeCheck(index); Node temp = node(index); return temp.element; } public void rangeCheck(int index) throws Exception { if(index<0||index>=size){ throw new Exception(index +" out of " + size); } } /* * 获取指定位置的节点 */ private Node node(int index){ Node temp = null; if(index > (size >> 1)){//二分法查询 temp = last; for (int i = size -1; i > index; i--) { temp = temp.previous; } }else{ temp = first; for(int i = 0;i< index;i++){ temp = temp.next; } } return temp; } /** * 删除元素 * @param index * @return * @throws Exception */ public Object remove(int index) throws Exception{ rangeCheck(index); Node temp = node(index); if(temp.previous==null && temp.next == null){ first = last = null; }else if(temp.previous==null){ temp.next.previous = null; first = temp.next; }else if(temp.next==null){ temp.previous.next = null; last = temp.previous; }else{ temp.previous.next = temp.next; temp.next.previous = temp.previous; } size--; return temp; } public Iterator iterator(){ return new Iter(); } @SuppressWarnings({ "unused", "rawtypes" }) private class Iter implements Iterator { private int cursor; private int lastRet; @Override public boolean hasNext() { return cursor!=size(); } @Override public Object next() { Object temp = null; try { temp = get(cursor); lastRet = cursor++; } catch (Exception e) { e.printStackTrace(); } return temp; } @Override public void remove() { if(lastRet ==-1){ throw new IllegalArgumentException(); } try { SimulateLinkedList.this.remove(lastRet); //if(lastRet < cursor){ cursor--; //} lastRet = -1; } catch (Exception e) { e.printStackTrace(); } } } //节点 class Node{ private Node previous; private Object element; private Node next; public Node(Node previous, Object element, Node next) { super(); this.previous = previous; this.element = element; this.next = next; } public Node getPrevious() { return previous; } public void setPrevious(Node previous) { this.previous = previous; } public Object getElement() { return element; } public void setElement(Object element) { this.element = element; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } } }
测试类
public class TestSimulateLinkedList { private SimulateLinkedList list = null; @BeforeClass public static void setUpBeforeClass() throws Exception { } @AfterClass public static void tearDownAfterClass() throws Exception { } @Before public void setUp() throws Exception { list = new SimulateLinkedList(); list.add("aaa"); list.add(111); list.add(222); list.add(333); } @After public void tearDown() throws Exception { for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } list = null; } @Test public void testAdd() { System.out.println(list.size()); } @Test public void testAddEx() { list.add(1,543); System.out.println(list.size()); System.out.println("========================"); } @Test public void testGet() throws Exception { System.out.println(list.get(1)); } @Test public void testRemove() throws Exception { list.remove(0); System.out.println(list.size()); } @Test public void testIterRemove() throws Exception { Iterator iter = list.iterator(); while(iter.hasNext()){ System.out.println(iter.next()); iter.remove(); } System.out.println("================="+list.size()); } }
“
相关文章推荐
- 电梯导航
- python转换时间戳为 字符串
- Java使用HttpURLConnection上传文件
- HttpClient使用详解(http伪造文件上传请求)
- 使用Eclipse-Maven-git做Java开发(4)--关于eclipse的更详细介绍
- redis配置文件redis.conf详细说明
- 代码注释标准大全
- Fork/Join框架简析
- CSS从大图中抠取小图完整教程(background-position应用)
- tab 菜单中 出现分页的情况下 twbs-pagination 解绑和重新绑定
- apache StringUtils
- Ext3跨域ScriptTagProxy使用
- cmake 之一个小例子
- BOOKSTORE OPENCART 自适应主题模板 ABC-0109
- mycncart 新闻系统
- 加载指定包名下的全部类以及指定annotation进行过滤的工具类
- [leedcode 241] Different Ways to Add Parentheses
- Mac下启动和停止Mysql服务
- libqrencode生成二维码图片的问题
- Javascript 面向对象编程(一):封装