Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 迭代器和抛出异常_Java_Algorithm_Iterator_Stack_Queue - Fatal编程技术网

Java 迭代器和抛出异常

Java 迭代器和抛出异常,java,algorithm,iterator,stack,queue,Java,Algorithm,Iterator,Stack,Queue,我试图做一个双端的德克,但我一直遇到错误,坦白说,我不知道如何解决。第一个是关于德克的。在迭代器函数中,我不断出现以下错误,我不知道原因: Deque.java:105: error: incompatible types: Deque.DequeIterator cannot be converted to Iterator<Item> return new DequeIterator(); 这是我的密码: import java.util.Iterator;

我试图做一个双端的德克,但我一直遇到错误,坦白说,我不知道如何解决。第一个是关于德克的。在迭代器函数中,我不断出现以下错误,我不知道原因:

Deque.java:105: error: incompatible types: Deque.DequeIterator cannot be converted to Iterator<Item>
    return new DequeIterator();
这是我的密码:

    import java.util.Iterator;
    import java.util.NoSuchElementException;

    public class Deque<Item> implements Iterable<Item>{

      private int size;

      private Node<Item> first;
      private Node<Item> last;

      private class Node<Item>{
        Item item;
        Node<Item> next;
        Node<Item> prev;

        Node(Item item) {
                this.item = item;
                next = null;
                prev = null;
            }
      }

      // construct an empty deque
      public Deque(){
        first = null;
        last = null;
        size = 0;
      }

      public boolean isEmpty(){return size == 0;} // is the deque empty?
      public int size(){return size;}              // return the number of items on the deque

      // add the item to the front
      public void addFirst(Item item){
        if (item == null){
          throw new java.lang.NullPointerException();
        }
        else if (this.isEmpty()){
          first = new Node(item);
          first = last;
        }
        else{
          Node oldfirst = first;
          Node first = new Node(item);
          first.next = oldfirst;
          oldfirst.prev = first;
        }
        size ++;
      }

      // add the item to the end
      public void addLast(Item item){
        if (item == null){
          throw new java.lang.NullPointerException();
        }
        else if (this.isEmpty()){
          Node last = new Node(item);
          last = first;
        }
        else{
          Node oldlast = last;
          Node last = new Node(item);
          oldlast.next = last;
          last.prev = oldlast;
        }
        size ++;
      }

      // remove and return the item from the front
      public Item removeFirst(){
        if (this.isEmpty()){
          throw java.util.NoSuchElementException();
        }
        else{
          Item item = first.item;
          first = first.next;
          first.prev = null;
          if (size == 1){
            first = last;
          }
          size --;
          return item;
        }
      }

      // remove and return the item from the end
      public Item removeLast(){
        if (this.isEmpty()){
          throw java.util.NoSuchElementException();
        }
        else{
          Item item = last.item;
          last = last.prev;
          if (size == 1){
            last = first;
          }
          size --;
          return item;
        }
      }

      // return an iterator over items in order from front to end

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


      private class DequeIterator<Item> implements Iterable<Item>{
        private Node current;

        public DequeIterator() { this.current = first;}

        public boolean hasNext(){ return current != null;};
        public void remove() {throw new UnsupportedOperationException();}
        public Item next(){
          if (!hasNext()) throw new NoSuchElementException();
          Item item = current.item;
          current = current.next;
          return item;
        }

      }



      // unit testing (optional)
      public static void main(String[] args){
        Deque<String> deque = new Deque<String>();
        deque.addFirst("1");
        //StdOut.println("addfirst to string: " + deque.AXCToString());
        deque.addFirst("2");
        //StdOut.println("addfirst to string: " + deque.AXCToString());
        deque.addFirst("3");
        //StdOut.println("addfirst to string: " + deque.AXCToString());
        deque.addFirst("4");
        //StdOut.println("addfirst to string: " + deque.AXCToString());
        deque.addFirst("5");
      }
    }
import java.util.Iterator;
导入java.util.NoSuchElementException;
公共类Deque实现了Iterable{
私有整数大小;
私有节点优先;
最后是私有节点;
私有类节点{
项目;
节点下一步;
节点前置;
节点(项目){
this.item=项目;
next=null;
prev=null;
}
}
//构造一个空的deque
公共部门(){
第一个=空;
last=null;
尺寸=0;
}
public boolean isEmpty(){return size==0;}//数据块是否为空?
public int size(){return size;}//返回数据块上的项数
//将项目添加到前面
公共无效添加第一个(项目){
如果(项==null){
抛出新的java.lang.NullPointerException();
}
else if(this.isEmpty()){
第一个=新节点(项目);
第一个=最后一个;
}
否则{
节点oldfirst=第一个;
第一个节点=新节点(项目);
first.next=oldfirst;
oldfirst.prev=第一个;
}
大小++;
}
//将项目添加到末尾
公共无效addLast(项目){
如果(项==null){
抛出新的java.lang.NullPointerException();
}
else if(this.isEmpty()){
最后一个节点=新节点(项目);
最后=第一;
}
否则{
节点oldlast=last;
最后一个节点=新节点(项目);
oldlast.next=last;
last.prev=oldlast;
}
大小++;
}
//从前面取出并退回物品
公共项目移除优先(){
if(this.isEmpty()){
抛出java.util.NoSuchElementException();
}
否则{
项目=第一个。项目;
first=first.next;
first.prev=null;
如果(大小==1){
第一个=最后一个;
}
大小--;
退货项目;
}
}
//从末端移除并返回项目
公共项目removeLast(){
if(this.isEmpty()){
抛出java.util.NoSuchElementException();
}
否则{
项目=最后一个项目;
last=last.prev;
如果(大小==1){
最后=第一;
}
大小--;
退货项目;
}
}
//按从头到尾的顺序返回项的迭代器
公共迭代器迭代器(){
返回新的DequeIterator();
}
私有类DequeIterator实现Iterable{
专用节点电流;
public DequeIterator(){this.current=first;}
公共布尔值hasNext(){返回当前值!=null;};
public void remove(){抛出新的UnsupportedOperationException();}
公共项目下一步(){
如果(!hasNext())抛出新的NoSuchElementException();
项目=当前项目;
当前=当前。下一步;
退货项目;
}
}
//单元测试(可选)
公共静态void main(字符串[]args){
Deque Deque=新Deque();
deque.addFirst(“1”);
//println(“addfirst to string:+deque.AXCToString());
deque.addFirst(“2”);
//println(“addfirst to string:+deque.AXCToString());
deque.addFirst(“3”);
//println(“addfirst to string:+deque.AXCToString());
deque.addFirst(“4”);
//println(“addfirst to string:+deque.AXCToString());
deque.addFirst(“5”);
}
}

您的第一个问题是您的
DequeIterator
类实现了
Iterable
,而它应该实现
Iterator
Iterable
通常用于集合之类的东西,这样就可以提供一个
迭代器
实例。看起来您已经实现了迭代器的方法,所以这应该只是将行更改为:

private class DequeIterator<Item> implements Iterator<Item> {
此外,Java的标准做法是使用导入,而不是使用绝对路径,而且看起来您已经导入了它,因此您可以将其缩短为:

throw new NoSuchElementException();
throw new java.util.NoSuchElementException();
throw new NoSuchElementException();