Java自制迭代器ConcurrentModificationException

时间:2019-01-10 14:32:47

标签: java exception iterator

我想为循环链表实现一个迭代器,但是当用户尝试修改当前正在迭代的迭代器时,我找不到如何实现ConcurrentModificationException的功能。

我知道我必须更改next和hasNext函数,但是我不知道该怎么做。

我试图搜索迭代器的java doc源代码,以帮助我理解这一点,但我所能找到的只是带有功能名称和使用方法的文档的“源代码” ...

    public Iterator<Item> iterator() {
        return new ListIterator();
    }

    // an iterator, doesn't implement remove() since it's optional
    private class ListIterator implements Iterator<Item> {
        private Node current = last.next; //initialize as the first node
        public boolean hasNext(){
            return current == null;
        }
        public Item next(){
            if(hasNext() == false) throw new NoSuchElementException();
            Item item = current.item;
            current = current.next;
            return item;
        }
        public void remove(){
            throw new UnsupportedOperationException();
        }


    }

0 个答案:

没有答案