我只是做一些練習,從我的書之一,我很好奇爲什麼我收到以下錯誤在日食:無法將節點<E>轉換成節點<E>?
Type mismatch: cannot convert from type DoublyLinkedList.Node<E> to DoublyLinkedList.Node<E>
代碼:
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
public class DoublyLinkedList<E extends Comparable<E>> implements Iterable<E>{
private int size = 0;
private Node<E> head;
private Node<E> tail;
/** Returns a list iterator object for the list at
* the specified index
*/
public DoublyLinkedList(){
}
private static class Node<E> {
Node<E> next = null;
Node<E> prev = null;
E data;
public Node(E dataItem){
data = dataItem;
}
public Node(E dataItem, Node<E> previous, Node<E> nextNode){
this(dataItem);
prev = previous;
next = nextNode;
}
}
private class MyListIter<E> implements ListIterator<E>{
private Node<E> lastReturned; // a link reference to the last item that was returned
private Node<E> nextItem; // a link reference to the next item in the list
/** The index of the current position */
private int index = 0;
public MyListIter(int pos){
if (pos < 0 || pos > size)
throw new IndexOutOfBoundsException("Invalid index: " + index);
lastReturned = null;
if (pos == size){
index = size;
nextItem = null;
} else { // otherwise we will start at the beginning of the list, and loop until the position in the argument
nextItem = head; // ERROR
for (index = 0; index < pos; index++){
nextItem = nextItem.next; // next item will always reference the list node that is called by the next method
}
}
}
@Override
public void add(E element) {
if (head == null){
Node<E> newNode = new Node<E>(element);
head = newNode; // ERROR
tail = head;
}
}
@Override
public boolean hasNext() {
return nextItem != null; // just checks to make sure there is a node following the current node
}
@Override
public boolean hasPrevious() {
return (nextItem == null && size != 0) || nextItem.prev != null;
}
@Override
public E next() {
if (!hasNext())
throw new NoSuchElementException("There is no node at that location");
lastReturned = nextItem;
nextItem = nextItem.next;
index++;
return lastReturned.data;
}
@Override
public int nextIndex() {
// TODO Auto-generated method stub
return 0;
}
@Override
public E previous() {
if (!hasPrevious())
throw new NoSuchElementException();
if (nextItem == null) // the iterator is at the end of the list
nextItem = tail; // therefore, the nextItem is at the tail, so the previous is the tail. ERROR HERE TOO
else
nextItem = nextItem.prev;
lastReturned = nextItem;
index--;
return lastReturned.data;
}
@Override
public int previousIndex() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void remove() {
// TODO Auto-generated method stub
}
@Override
public void set(E arg0) {
// TODO Auto-generated method stub
}
}
@Override
public Iterator<E> iterator() {
// TODO Auto-generated method stub
return null;
}
}
我作了評論正是我在3個不同的位置得到錯誤。如果你能提供任何反饋,我會很感激。我的書沒有解決它,我已經搜索過,並且看起來無法得到我正在尋找的答案。
+1好點關於'E extends Comparable super E>' –
哇。我永遠不會知道。我認爲這固定了一切。這本書寫得很糟糕。我的印象是,如果DoublyLinkedList類型爲,則該類別中的所有內容也應該是,使它們全部屬於同一類型。但我猜它不是? –
TMGunter
@TMGunter:如果聲明瞭一個不帶'static'關鍵字的內部類,則該內部類的所有實例都必須屬於包含類的特定_instance_。因此,它們自動訪問爲包含類聲明的泛型類型(在這種情況下爲'E')。我實際上不確定'Node'在這裏是一個問題,因爲它有'static'關鍵字,因此它是一個嵌套類,它不會繼承'E'的定義。 'MyListIter'在這裏可能是真正的問題,因爲它繼承了'E'的定義,但也聲明瞭它自己的(衝突的)定義。 – ColinD