链表的自定义迭代器

时间:2017-03-05 10:16:42

标签: java for-loop iterator

我已经创建了自定义链接列表。我也为它做了一个迭代器。但是正如我所理解的,程序并没有在方法size()中循环。它有什么问题?是关于我的错误循环还是iterator()。每个节点都有字段:nextIndexprevIndexvalue

public class CustomLinkedList<E> implements List<E>{
    private ListNode<E> header = new ListNode<>();

    public CustomLinkedList() {
        this.header = new ListNode();
    }

    @Override
    public int size() {
        int size = 0;
        for(E it : this) {
            size++;
        }
        return size;
    }

    @Override
    public boolean isEmpty() {
        if(this.header == null){
            return true;
        } else {return false;}
    }

    @Override
    public boolean contains(Object o) {
        if(((Integer) header.getValue() == 42) && ((Integer) header.getNextIndex().getValue() == 38)) return true;
        return false;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            private ListNode it = header;

            @Override
            public boolean hasNext() {
                return (it.getNextIndex() != header) && (it.getNextIndex() != null);
            }

            @Override
            public E next() {
                it = it.getNextIndex();
                return (E) it.getValue();
            }

            @Override
            public void remove()
            {
                throw new UnsupportedOperationException("Iterator is read-only");
            }
        };
    }


    @Override
    public boolean add(E e) {
        ListNode<E> temp = new ListNode<>();
        temp.setValue(e);
        if (this.size() < 1) {
            header.setValue(e);
            header.setPrevIndex(header);
            header.setNextIndex(header);
        } else {
            temp.setNextIndex(header);
            ListNode tempLast = header.getPrevIndex();
            tempLast.setNextIndex(temp);
            header.setPrevIndex(temp);
            temp.setPrevIndex(tempLast);
        }
        return true;
    }

    @Override
    public boolean remove(Object o) {
        return true;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        throw new NotImplementedException();
    }

    @Override
    public void clear() {
        throw new NotImplementedException();
    }

    @Override
    public E get(int index) {
        throw new NotImplementedException();
    }

    @Override
    public int indexOf(Object o) {
        throw new NotImplementedException();
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        throw new NotImplementedException();
    }
}

2 个答案:

答案 0 :(得分:1)

您从header.getNextIndex() == null开始,然后拨打add(E e)一次。 在add()内,size()将返回0,因此您设置header.setPrevIndex(header)header.setNextIndex(header)

接下来,再次致电add(E e) . Inside添加(),尺寸()will call hasNext()and hasNext()will check (it.getNextIndex() != header) && (it.getNextIndex() != null)将返回 false ,因为it.getNextIndex() == header 因此 size()将始终小于一个

您的代码中还有很多其他错误。更好的部分实现将类似于

public class CustomLinkedList<E> implements List<E> {

    private ListNode<E> header;

    public CustomLinkedList() {
        this.header = null;
    }

    @Override
    public int size() {
        int size = 0;
        for (E it : this)
            size++;
        return size;
    }

    @Override
    public boolean isEmpty() {
        return header == null;
    }

    @Override
    public boolean contains(Object obj) {
        if (header != null)
            for (E it : this)
                if (it.equals(obj))
                    return true;
        return false;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            private ListNode<E> it = null;

            @Override
            public boolean hasNext() {
                if (it==null && header!=null)
                    return true;
                else if (it != null)
                    return it.getNextIndex() != null;
                else
                    return false;
            }

            @Override
            public E next() {
                if (it==null)
                    it = header;
                else
                  it = it.getNextIndex(); 
                return it.getValue();
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("Iterator is read-only");
            }
        };
    }

    @Override
    public boolean add(E e) {
        ListNode<E> temp = new ListNode<>();
        temp.setValue(e);
        if (header == null) {
            header = temp;
        } else {
            ListNode<E> it = header;
            while (it.getNextIndex() != null)
                it = it.getNextIndex();
            temp.setPrevIndex(it);
            it.setNextIndex(temp);
        }
        return true;
    }

}

答案 1 :(得分:0)

您需要使用hasNext的{​​{1}}和next方法来遍历元素并增加大小,例如:

iterator