在不使用getEntry的情况下为java中的Linked List创建迭代器

时间:2014-11-15 03:24:02

标签: java iterator

我正在尝试创建一个适用于linkedList的迭代器。我已经创建了链接列表,我认为它应该工作正常,但现在我需要帮助创建迭代器。我试图使它比只使用带有for循环的getEntry()更快,每次增加1,因为使用该方法意味着我必须遍历每个元素的链表。我试图更快地解决它,但不知道从哪里开始。我知道我需要创建next和hasnext方法,但不知道如何。还坚持构造函数和实例方法。

这是我到目前为止的代码:

import java.util.NoSuchElementException;


public class SListIterator<T>
{
    private Node firstNode;
    private int numberOfEntries;

    public SListIterator()
    {
        firstNode = null;
        numberOfEntries = 0;
    }

    public void addToFirst(T aData)
    {
        firstNode = new Node(aData, firstNode);
        numberOfEntries++;
    }

    public T getEntry(int givenPosition)
    {
        T result = null;

        if((givenPosition >= 1) && (givenPosition <= numberOfEntries))
        {
            result = (getNodeAt(givenPosition)).data;
        }

        return result;
    }

    private Node getNodeAt(int givenPosition)
    {
        Node currentNode = firstNode;

        for(int counter = 1; counter < givenPosition; counter++)
        {
            currentNode = currentNode.next;
        }

        return currentNode;
    }

    public Iterator<T> getIterator()
    {
        // TO DO        
    }

    private class IteratorForSList implements Iterator<T>
    {
        // instance variable for IteratorForSList       


        private IteratorForSList()
        {
            // constructor
        }

        public boolean hasNext()
        {
            // need help
        }

        public T next()
        {
            // need help
        }

        public T remove()
        {
            throw new UnsupportedOperationException("remove() is not supported by this iterator");
        }
    }

    private class Node
    {
        private T data;
        private Node next;

        private Node(T aData, Node nextNode)
        {
            data = aData;
            next = nextNode;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您的迭代器只需要知道链表中的当前节点:

public Iterator<T> getIterator() {
  return new IteratorForSList(firstNode);
}

private class IteratorForSList implements Iterator<T> {
  private Node currentNode;

  private IteratorForSList(Node list) {
    currentNode = list;
  }

  public boolean hasNext() {
    return currentNode != null;
  }

  public T next() {
    T result = currentNode.data;
    currentNode = currentNode.next;
    return result;
  }

  public void remove() {
    throw new UnsupportedOperationException(
        "remove() is not supported by this iterator");
  }
}

答案 1 :(得分:0)

为什么不使用java.util.LinkedList?它有一个迭代器方法,它返回一个迭代器。

import java.util.Iterator;
import java.util.LinkedList;


public class SListIterator{
    public static void main(String[] args){
        LinkedList<Integer> list = new LinkedList<Integer>();
        list.add(0);
        list.add(1);
        list.add(2);
        list.add(3);

        Iterator<Integer> iter = list.iterator();
        while(iter.hasNext()){
            System.out.println(iter.next());
        }
    }
}