问题
this is a code i have written for implementing linked list,
private class DequeIterator<Item> implements Iterable<Item> {
private Node pElement;
DequeIterator() {
pElement = first;
}
public boolean hasNext() {
return pElement != null;
}
public Item next() {
if (!this.hasNext()) {
throw new NoSuchElementException();
}
Item ret = pElement.it;
pElement = pElement.next;
return ret;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
I dont know what is going wrong i am getting follwing errors please help me resolve those
=> Deque.java:25: error: Deque.DequeIterator is not abstract and does not override abstract method iterator() in Iterable
=>Deque.java:35: error: incompatible types Item ret = pElement.it; ^ required: Item#2 found: Item#1
=>Deque.java:121: error: incompatible types return new DequeIterator(); ^ required: Iterator found: Deque.DequeIterator
回答1:
You are implementing the wrong interface. Iterable requires that you implement the iterator() method, which you are not providing. Thus the first error.
You probably want to implement Iterator instead, which requires hasNext(), next() and remove().
You didn't show the rest of your code, but I presume you have Deque.java which has the rest of the linked list code (i.e. a reference to the head etc.), so probably you might want to have Deque implements Iterable<Item> with the iterator() method returning an instance of DequeIterator.
You also need to remove the <Item> from the DequeueIterator. You are already saying it implements Iterable<Item>, so you are restricting the type. You are changing it to a generic type instead, which is the reason for the second error. I presume that Item is a concrete class defined somewhere else (especially since pElement returns it and does not have a generic type associated with it).
The third error, no idea what's in line 121, you didn't include it. But most probably its for the same reason I mentioned above (DequeIterator needs to implement Iterator not Iterable)
回答2:
Looks like your internal DequeIterator class is overriding the <Item> generic from the top class Deque<Item>. Just remove the <Item> from DequeIterator definition. Also, the class that should implement Iterable must be your Deque class, and DequeIterator must implement Iterator.
public class Deque<Item> implements Iterable<Item> {
//implementation...
private class DequeIterator implements Iterator<Item> {
//implementation...
}
}
回答3:
Deque.java:25: error: Deque.DequeIterator is not abstract and does not override abstract method iterator() in Iterable
In class Iterable exist method iterator(), which is abstract. You are responsible for implementing every abstract method.
回答4:
You have to override the iterator() method in your class DequeIterator<Item> as it implements Iterable interface.
hasNext(), next(), remove() - these methods are in Iterator interface.
回答5:
Your Iterator implements Iterable instead of implementing Iterator. Always add the @Override annotation on methods which are supposed to override a base class or interface method.
来源:https://stackoverflow.com/questions/22357259/correct-way-to-override-iterablesobj-in-java