泛型集合实现java编译错误
编辑:
public class LinkedList<E> {
private class Node {
protected Node next, prev;
protected E data;
protected Node(E dat) {
data = dat;
next = prev = null;
}
}
private Node head, tail;
public LinkedList() {
(head = new Node(null)).next = tail = new Node(null);
tail.prev = head;
tail.next = head.prev = null;
}
public class LinkedListIterator {
private Node current = null;
public synchronized void resetToHead() {
current = head.next;
}
public synchronized void resetToTail() {
current = tail.prev;
}
public synchronized E get() {
if (current!=null) return current.data;
return null;
}
}
}
问题是我在强调的行上收到以下编译错误:
> Type mismatch: cannot convert from LinkedList<E>.Node<E> to
> LinkedList<E>.Node<E>
这是什么意思?我该如何解决这个问题?
顺便说一句,代码只是实现的一部分,所以不要试图从逻辑上弄清楚它。
EDIT :
public class LinkedList<E> {
private class Node {
protected Node next, prev;
protected E data;
protected Node(E dat) {
data = dat;
next = prev = null;
}
}
private Node head, tail;
public LinkedList() {
(head = new Node(null)).next = tail = new Node(null);
tail.prev = head;
tail.next = head.prev = null;
}
public class LinkedListIterator {
private Node current = null;
public synchronized void resetToHead() {
current = head.next;
}
public synchronized void resetToTail() {
current = tail.prev;
}
public synchronized E get() {
if (current!=null) return current.data;
return null;
}
}
}
the problem is that i get the following compilation Error on the emphasized lines :
> Type mismatch: cannot convert from LinkedList<E>.Node<E> to
> LinkedList<E>.Node<E>
what does it mean? and how do i fix this?
btw, the code is only part of the implementation so dont try to logicly figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
--- 随着问题略有变化而编辑 ---
现在的问题是,我如何让两个内部类协调泛型类型?简而言之,如果它们都是绑定泛型类型的外部类的内部类,则不必这样做。因此,即使使用非泛型 LinkedListIterator 中的 public synchronized E get() ,您也会返回一个
E
(并且它是类型安全的)。但是,如果您随后尝试实现
java.util.Iterator
,事情就会崩溃,因为 E 基于不同的类(接口),因此E
具有不同的范围。你如何解决这个问题?您需要将Node
类参数化为Node
以满足E
绑定存在于Iterator
即使该实现在其原始类的范围之外使用。这强制静态定义Node
。它强制
Node
进行静态定义的原因与垃圾收集有关。即使LinkedList
已计划进行垃圾回收,迭代器仍可能保留对 Node 的引用。当然,您也许能够通过特定的实现来防止这种情况发生,但 JVM 必须允许任何实现(甚至是错误的实现)。也许用代码来解释会更容易,
如果你不小心,你现在可能会回到开始的地方;然而,关键是在父作用域中需要时构造新的
Node
对象。由于这与构造 LinkedIterator 类型的范围相同,因此将确保泛型类型安全。--- 原帖如下 ----
通过指定您的节点类定义是
Node
,您基本上创建了第二个独立作用域的泛型类型E
,它将隐藏LinkedList
类中的外部泛型类型E
。由于您的类都不是静态的,因此它们仅存在于 LinkedList 类的上下文中,该类将提供泛型绑定。这意味着您可以将
Node
简化为Node
,但仍将E
类类型放入Node
类中。LinkedListIterator
也是如此,只不过如果您希望它实现Iterator
,您应该指示它实现Iterator
。由于请求,以下是在我的机器上编译的代码,(java 1.6.0_20)
--- Edited as the question changes slightly ---
The question is now becoming, how do I have two inner classes coordinate generic types? In short, they don't have to if they are both inner classes of an outer class where the generic type is bound. So even with the
public synchronized E get()
in the non-genericLinkedListIterator
you are returning anE
(and it is type safe).However, if you then reach out to implement
java.util.Iterator<E>
things fall apart, because that E is based on a different class (interface) so theE
has different scoping. How do you fix this? You need to parameterize yourNode
classes toNode<E>
to satisfy thatE
bindings exist on the implementation ofIterator
even when that implementation is being used outside of the scope of it's originating class. This forcesNode<E>
to be defined statically.The reason it forces the static definition of
Node<E>
has to do with garbage collection. An Iterator might still be holding references toNode
s even though theLinkedList
is scheduled for garbage collection. Sure, you might be able to keep such a thing from happening with a specific implementation, but the JVM has to allow any implementation (even an errant one).Perhaps it is easier to explain with code
If you are not careful, you can now wind up back where you started; however, the key is to construct new
Node<E>
objects when needed in the parent scope. Since that is the same scope where you construct LinkedIterator types, the generic type safety will be ensured.--- Original post follows ----
By specifying that your node class definition is a
Node<E>
, you basically create a second, independently scoped generic typeE
which will hide the outer generic typeE
in theLinkedList
class.Since none of your classes are static, they will only exist within context of a LinkedList class, which will provide the generics binding. That means you can simplify
Node<E>
toNode
yet still putE
class types within theNode
class. Same goes for theLinkedListIterator
, except that if you want it to implementIterator
you should indicate it implementsIterator<E>
.Due to request, what follows is the code that compiles on my machine, (java 1.6.0_20)
通过参数化嵌入的类,您做得有点过头了。我删除了所有不必要的。
或者使用静态类 Node。
You overdid it a bit by parametrising the embedded classes. I removed all unnecessary ones.
Alternatively with a static class Node.
它不明白是在 LinkedListIterator 中是相同的作为父类。只需从内部类中删除:
It doesn't understand that the <E> in LinkedListIterator is the same <E> as the parent class. Just remove from the inner class: