Common use with inner class issues

I do not send all the code. I have it:

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; } } 

On javac -Xlint LinkedList2.java I get this error:

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

You can help?

+1
java generics
source share
1 answer

When you define your inner class, LinkedList2Iterator , you have shared it with another parameter of type <T> . That <T> does not match <T> from the outer class LinkedList2 .

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

You don't need to declare another <T> here, just use the <T> from the outer class, which is still in scope:

 private class LinkedList2Iterator implements Iterator<T> { 
+5
source share

All Articles