gpt4 book ai didi

java - 无法从 Node 转换为 Node

转载 作者:行者123 更新时间:2023-11-29 10:06:20 24 4
gpt4 key购买 nike

我只是从我的一本书中做一些练习,我很好奇为什么我在 eclipse 中出现以下错误:

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 个不同位置得到错误的确切位置。如果您能提供任何反馈,我将不胜感激。我的书没有解决这个问题,我四处搜索但似乎无法真正得到我正在寻找的答案。

最佳答案

您声明了两种不同的泛型类型:E (对于 Node )和 E extends Comparable<E> (对于 DoublyLinkedList )。

这里的主要问题可能是MyListIter ,这是一个非静态内部类,因此自动继承 DoublyLinkedList E 的定义.因为它继承了E的定义,您应该将其声明为

private class MyListIter implements ListIterator<E>

但你做到了MyListIter<E> ,它正在重新定义 EE 不同的东西那DoublyLinkedList用户(隐式 E extends ObjectE extends Comparable<E> )。

认为 Node应该按原样工作,因为它是一个嵌套类(带有 static 关键字)并且不继承 E 的定义来自 DoublyLinkedList .但是,在这里将其声明为 DoublyLinkedList 的非静态内部类可能是有意义的。 ( private class Node ) 与 MyListIter 相同.

此外,您可能应该允许 E成为某种实现了 Comparable 的类型的子类型通过将其声明为 E extends Comparable<? super E> .

关于java - 无法从 Node<E> 转换为 Node<E>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7140866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com