AbstractList的迭代器remove()方法疑问

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值