[英]error: method does not override or implement a method from a supertype
Building queue implementation based on linked list. 基于链表的构建队列实现。 Cannot run application because of the two errors:
由于出现两个错误,因此无法运行应用程序:
public class Queue<Integer> implements Iterable<Integer> {
...
public Iterator<Integer> iterator() {
return new ListIterator(first);
}
private class ListIterator<Integer> implements Iterator<Integer> {// error #1
private Node<Integer> current;
public ListIterator(Node<Integer> first) {
current = first;
}
public boolean hasNext(){ return current != null; }
public void remove() { throw new UnsupportedOperationException();}
public int next() { // error #2
if (!hasNext()) throw new NoSuchElementException();
int item = current.item;
current = current.next;
return item;
}
}
}
error #1: error: Queue.ListIterator is not abstract and does not override abstract method next() in Iterator where Integer is a type-variable: Integer extends Object declared in class Queue.ListIterator 错误#1:错误:Queue.ListIterator不是抽象的,并且不覆盖Iterator中Integer是类型变量的抽象方法next():Integer扩展了在Queue.ListIterator类中声明的对象
error #2: error: next() in Queue.ListIterator cannot implement next() in Iterator return type int is not compatible with Integer where E,Integer are type-variables: E extends Object declared in interface Iterator Integer extends Object declared in class Queue.ListIterator 错误#2:错误:Queue.ListIterator中的next()无法在Iterator中实现next()返回类型int与Integer不兼容,其中E,Integer是类型变量:E扩展了在接口Iterator中声明的对象Integer扩展了在类中声明的对象Queue.ListIterator
How to get it working? 如何使其运作?
Boxing and unboxing in Java simplify code in many places, but method return types is not one of them. Java中的装箱和拆箱在许多地方都简化了代码,但是方法返回类型不是其中之一。 The
next
method must return an Integer
, not an int
. next
方法必须返回Integer
而不是int
。 It must match the generic type parameter exactly. 它必须与通用类型参数完全匹配。
public Integer next()
Second, you've declared a generic type parameter Integer
in your Queue
and ListIterator
classes that has nothing to do with java.lang.Integer
. 其次,您在
Queue
和ListIterator
类中声明了一个通用类型参数Integer
,它与java.lang.Integer
无关。 Remove it: 去掉它:
// here
public class Queue implements Iterable<Integer> {
and 和
// here
private class ListIterator implements Iterator<Integer> {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.