无法从Node 转换为Node ?
我只是从我的一本书中做了一些练习,我很好奇为什么我在eclipse中得到以下错误:
Type mismatch: cannot convert from type DoublyLinkedList.Node to DoublyLinkedList.Node
码:
import java.util.Iterator; import java.util.ListIterator; import java.util.NoSuchElementException; public class DoublyLinkedList<E extends Comparable> implements Iterable{ private int size = 0; private Node head; private Node tail; /** Returns a list iterator object for the list at * the specified index */ public DoublyLinkedList(){ } private static class Node { Node next = null; Node prev = null; E data; public Node(E dataItem){ data = dataItem; } public Node(E dataItem, Node previous, Node nextNode){ this(dataItem); prev = previous; next = nextNode; } } private class MyListIter implements ListIterator{ private Node lastReturned; // a link reference to the last item that was returned private Node 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 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 newNode = new Node(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 iterator() { // TODO Auto-generated method stub return null; } }
我评论了我在3个不同位置得到错误的确切位置。 如果您能提供任何反馈,我会很感激。 我的书没有解决它,我已经搜索过,似乎无法得到我正在寻找的答案。
您已经声明了两种不同的generics类型: E
(用于Node
)和E extends Comparable
(用于DoublyLinkedList
)。
这里的主要问题可能是MyListIter
,它是一个非静态的内部类,因此自动inheritanceDoublyLinkedList
的E
定义。 因为它inheritance了E
的定义,所以你应该将其声明为
private class MyListIter implements ListIterator
但是你已经把它MyListIter
,它将E
重新定义为不同于DoublyLinkedList
用户的E
(隐式E extends Object
vs. E extends Comparable
)。
我认为 Node
应该按原样工作,因为它是一个嵌套类(使用static
关键字)并且不从DoublyLinkedList
inheritanceE
的定义。 但是,这里将它声明为与DoublyLinkedList
相同的DoublyLinkedList
( private class Node
)的非静态内部类DoublyLinkedList
是MyListIter
。
此外,您应该允许E
成为某种类型的子类型,通过将其声明为E extends Comparable super E>
来实现Comparable
E extends Comparable super E>
E extends Comparable super E>
。
看起来您收到此错误是因为您在Node
嵌套类中重新定义了E
由于它是静态嵌套类,因此它与父类DoublyLinkedList
没有直接关系。 使类非静态化以使E
继续在其中具有意义可能更有意义。 例如:
private class Node { Node next = null; Node prev = null; E data; ...
编辑:正如ColinD所说, MyListIter
同样不应该将E
重新声明为类型参数。 像Node
一样改变它应该可以解决这个问题。
ColinD是对的(+1)。
要了解发生了什么,想象不要使用相同的forms类型参数3次,但是E表示DoublyLinkedList,F表示节点,G表示MyListIter。 然后错误消息会说Type mismatch: cannot convert from type DoublyLinkedList.Node
。 解决方案是ColinD建议的解决方案。 如果需要,可以将Node
保留为静态,修复所有实例将具有相同的实际类型参数。
- Java foreach循环:for(整数i:列表){…}
- 在Java TreeMap中查找元素位置
- ConcurrentModificationException(Java)
- iterator hasnext()返回true但next()抛出NoSuchElementException
- 在Java中覆盖Iterables 的正确方法
- 如何在没有ConcurrentModificationException的情况下对Collection 进行交互并修改其项目?
- 使用Iterator循环时将项添加到HashMap
- java.util.ConcurrentModificationException&iteration?
- Java,使用Iterator搜索ArrayList并删除匹配的对象