1

我没有发布整个代码。我有这个:

public class LinkedList2<T extends Comparable<T>> implements Iterable<T> {

    private Node<T> head;
    private Node<T> tail;
    private int numOfElem;

    private class Node<T> {

        Node<T> next;
        T data;

        Node(Node<T> next, T data) {
            this.next = next;
            this.data = data;
        }
    }

    private class LinkedList2Iterator<T> implements Iterator<T> {
            private int count = LinkedList2.this.numOfElem;
            private Node<T> current = LinkedList2.this.head;
    }
}       

javac -Xlint LinkedList2.java收到此错误:

LinkedList2.java:134: incompatible types
found   : LinkedList2<T>.Node<T>
required: LinkedList2<T>.Node<T>
        private Node<T> current = LinkedList2.this.head;
                                              ^
1 error

你能帮我吗?

4

1 回答 1

5

当你定义你的内部类LinkedList2Iterator时,你已经用另一个<T>泛型类型参数使它成为泛型。这与来自外部类的<T>不匹配。<T>LinkedList2

private class LinkedList2Iterator<T> implements Iterator<T> {

您不需要<T>在这里声明另一个,只需使用<T>外部类中的,它仍在范围内:

private class LinkedList2Iterator implements Iterator<T> {
于 2013-06-29T00:20:31.563 回答