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

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


你能帮我吗?

最佳答案

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

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


您无需在此处声明另一个<T>,只需使用外部类中的<T>,该类仍在范围内:

private class LinkedList2Iterator implements Iterator<T> {

09-26 11:01