开发者

Reversible iterator

开发者 https://www.devze.com 2023-02-21 19:07 出处:网络
public ReversibleIterator iterator(); can anyone help me make this method? ill put up what i have done so far

public ReversibleIterator iterator();

can anyone help me make this method? ill put up what i have done so far

The ReversibleIterator should behave as follows. The first call to next or previous should return the first or last element of the list, respectively. Subsequent calls to next/previous should return the element that is next/previous with respect to the antecedent call to next/p开发者_StackOverflow社区revious. For example, if two calls to next result in Sunday and Monday, then a following call to previous should return Sunday.

public ReversibleIterator<T> iterator() {
    PublicLinkedList<T> list = new PublicLinkedList<T>();
    PublicNode<T> node = list.head;
    while (node.getElement() != null) {
        list.add(node.getElement());
        node = node.getNext();
    }
    ReversibleIterator<T> rIter = new ReversibleIterator<T>(list);
    return rIter;
}


Java's ListIterator is what you need. The big advantage: It already exists.

You can retrieve it from any list in Java by calling the listIterator() function.

0

精彩评论

暂无评论...
验证码 换一张
取 消