无法从 Node转换到节点?
我只是在做我的一本书中的一些练习,我很好奇为什么我在 eclipse 中收到以下错误:
类型不匹配:无法从 DoublyLinkedList.Node
代码:
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 个不同位置收到错误的确切位置。如果您能提供任何反馈,我将不胜感激。我的书没有解决这个问题,我四处搜寻,似乎无法真正得到我正在寻找的答案。
I am just doing some practice from one of my books, and I was curious about why I am getting the following error in eclipse:
Type mismatch: cannot convert from type DoublyLinkedList.Node<E> to DoublyLinkedList.Node<E>
Code:
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;
}
}
I commented where exactly I am getting the error in 3 different locations. If you can provide any feedback, I'd appreciate it. My book doesn't address it and I have searched around and can't really seem to get the answer i'm looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您声明了两种不同的泛型类型:
E
(对于Node
)和E extends Comparable
(对于DoublyLinkedList
代码>)。这里的主要问题可能是
MyListIter
,它是一个非静态内部类,因此自动继承DoublyLinkedList
的E
定义。因为它继承了E
的定义,所以您应该将其声明为MyListIter
,这会重新定义E
与DoublyLinkedList
用户的E
不同(隐式E extends Object
与E extends可比较
)。我认为
Node
应该按原样工作,因为它是一个嵌套类(带有static
关键字)并且不继承<的定义来自DoublyLinkedList
的 code>E。但是,在这里将其声明为与MyListIter
相同的DoublyLinkedList
(private class Node
)的非静态内部类可能是有意义的>。另外,您可能应该允许
E
成为某种类型的子类型,通过将其声明为E 来实现
。Comparable
扩展比较You've declared two different generic types:
E
(forNode
) andE extends Comparable<E>
(forDoublyLinkedList
).The main issue here is probably
MyListIter
, which is a non-static inner class and as such automatically inheritsDoublyLinkedList
's definition ofE
. Because it inherits the definition ofE
, you should just be declaring it asbut you've made it
MyListIter<E>
, which is redefiningE
to something different than theE
thatDoublyLinkedList
users (implicitE extends Object
vs.E extends Comparable<E>
).I think
Node
should work as-is since it is a nested class (with thestatic
keyword) and doesn't inherit the definition ofE
fromDoublyLinkedList
. However, it'd probably make sense here to declare it as a non-static inner class ofDoublyLinkedList
(private class Node
) the same asMyListIter
.Also, you should probably allow
E
to be a type that is a subtype of some type that implementsComparable
by declaring it asE extends Comparable<? super E>
.看来您收到此错误是因为您在
Node
嵌套类中重新定义E
。由于它是一个静态嵌套类,因此它与父类DoublyLinkedList
没有直接关系。将类设置为非静态可能更有意义,以便E
继续在其中有意义。例如:编辑: 正如 ColinD 所指出的,
MyListIter
同样不应将E
重新声明为类型参数。使用Node
进行更改应该可以解决该问题。It looks like you're getting this error because you're redefining
E
in yourNode
nested class. Since it's a static nested class, it has no direct relationship to the parent classDoublyLinkedList
. It might make more sense to make the class non-static so thatE
continues to have meaning within it. For example:EDIT: as ColinD noted,
MyListIter
should similarly not redeclareE
as a type parameter. Changing this like withNode
should fix the issue.ColinD 是对的(+1)。
为了理解发生了什么,想象一下不使用相同的形式类型参数 3 次,而是使用 E 代表 DoublyLinkedList,F 代表 Node,G 代表 MyListIter。然后错误消息会显示保留为静态,修复后所有实例都将具有相同的实际类型参数。
Typemismatch:cannotconvertfromtypeDoublyLinkedList.Node;到 DoublyLinkedList.Node
。 ColinD 提出了解决方案。如果需要,您可以将 NodeColinD is right (+1).
To understand what's going on, imagine not using the same formal type parameter 3 times, but E for DoublyLinkedList, F for Node and G for MyListIter. Then the error message would say
Type mismatch: cannot convert from type DoublyLinkedList.Node<E> to DoublyLinkedList.Node<G>
. The solution is the one ColinD suggested. If you want, you can leaveNode<F>
static, with the fix all instances will have the same actual type parameter.