【发布时间】:2015-01-11 04:19:27
【问题描述】:
在遇到this(具有内部 Node 和 Iterator 类的 Generic LinkedList 的基本实现。下面粘贴的代码链接的剪辑版本)之前,我以为我已经掌握了泛型的窍门。
问:为什么我们有LinkedListIterator而不是LinkedListIterator<AnyType>
答:见答案here。
很好。说得通。但是,
问:为什么我们有Node<AnyType> 而不是Node?由于 Node 类也在 LinkedList 的范围内,从最后一个答案来看,我希望 Node 在其定义中也不需要。
public class LinkedList<AnyType> implements Iterable<AnyType>
{
private Node<AnyType> head;
//...
private static class Node<AnyType>
{
private AnyType data;
private Node<AnyType> next;
public Node(AnyType data, Node<AnyType> next)
{
this.data = data;
this.next = next;
}
}
public Iterator<AnyType> iterator()
{
return new LinkedListIterator();
}
private class LinkedListIterator implements Iterator<AnyType>
{
private Node<AnyType> nextNode;
public LinkedListIterator()
{
nextNode = head;
}
//...
【问题讨论】: