1

我正在实现一个带有链接的列表接口,但由于“ListADT”实现了 Iterable 接口。所以,我必须有一个方法来产生一个我不知道该怎么做的迭代器。我现在尝试使用它,当我为链表创建一个对象,然后调用 iterator() 方法时,我得到了溢出。我知道该方法应该生成一个 Iterator 对象,但不确定如何生成。

import java.util.Iterator;

public class LinkedList<T> implements ListADT<T> 
{ 
protected int count;
protected LinearNode <T> head, tail;
private  int modCount;

public LinkedList()
{
    count =0;
    head = tail= null;
}

 public T removeFirst()
 {
     T result = head.getElement();
     head = head.getNext();
     count--;
     return result;

 }

 public T removeLast()
 {
     // THROW EMPTY EXCEPTION

     T result;
     LinearNode <T> previous = null;
     LinearNode <T> current = head;
     while(!current.equals(tail))
     {
         previous = current;
         current = current.getNext();
     }
     result = tail.getElement();
     tail = previous;
     tail.setNext(null);
     count--;
     return result;

 }

 public T remove(T element)
 {
     // throw exception

     boolean found = false;
     LinearNode <T> previous = null;
     LinearNode <T> current = head;

     while (current != null && !found)
     {
         if(element.equals(current.getElement()))
             found = true;
         else
         {
             previous = current;
             current = current.getNext();
         }

         if (!found)
         {

         }
         else if (current.equals(head))
         {
             head = current.getNext();
         }
         else if(current.equals(tail))
         {
             tail = previous;
             tail.setNext(null);
         }
         else
             previous.setNext(current.getNext());
     }
     count --;
     return current.getElement();
 }

 public T first()
 {
    return head.getElement(); 
 }

 public T last()
 {
     return tail.getElement();
 }

 public boolean contains(T target)
 {
     boolean found = false;
     LinearNode <T> previous = null;
     LinearNode <T> current = head;

     while (current != null && !found)
     {
         if(target.equals(current.getElement()))
             found = true;
         else
         {
             previous = current;
             current = current.getNext();
         }
     }
     return found;
 }

 public boolean isEmpty()
 {
     boolean result = false;
     if( head == null && tail ==null)
     {
         result = true;
     }
     return result;
 }

 public int size()
 {
     return count;
 }

 public Iterator<T> iterator()
 {

    return this.iterator();
 }

 public String toString()
 {
     LinearNode <T> current = head;
     String result ="";
     String line = "";
     int loopCount=0;
     while(current != null)
     {
         loopCount++;
         line = loopCount + "> " + (String) current.getElement() + "\n";
         result = result + line;
         current = current.getNext();
     }
     return result;
 }
} 
4

1 回答 1

1

你的问题

你得到一个溢出,因为this.iterator()你的函数中的行public Iterator<T> iterator(),调用,你猜对了public Iterator<T> iterator()


方法一:懒惰的方式

如果你不打算为这个类使用迭代器,(这看起来像一个编程作业)你总是可以做超级超级懒惰的。

public Iterator<T> iterator() {
    throw new UnsupportedOperationException("Pffffft you don't need no iterator");
}

此处列出此方法只是为了完整性。鉴于您的链表没有其他方法可以访问中间的随机元素而不删除它前面或后面的所有内容,我建议您:

不要这样做


方法二:正确的方法

关于迭代器的事情是它们执行列表功能的特定子集,即hasNext()next()remove()。如果您不确定这三种方法的作用,我建议您查看http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html

您应该创建一个公共内部类。

public class LinkedList<T> implements ListADT<T> {
    ... stuff

    private class MyIterator<T> implements Iterator<T> {
        //It's best practice to explicitly store the head in the iterator
        private LinearNode<T> head;

        public MyIterator<T>(LinkedList<T>) {
            ...
        }

        @Override
        public boolean hasNext() {
            ...
        }

        @Override
        public T next() {
            ...
        }

        @Override
        public void remove() {
            ...
        }
    }

    public Iterator<T> iterator() {
         return new MyIterator<T>(this);
    }
} 

现在,如果您真的很聪明,您可以根据迭代器重写其余代码。笔记:

做这个

于 2013-10-17T15:47:09.173 回答