My compiler is JDK 6.0_65, and following are my codes (Deque.java):
import java.util.Iterator;
public class Deque<Item> implements Iterable<Item> {
private Node first;
private Node last;
private class Node {
Item value;
Node next;
}
public Deque(){} // construct an empty deque
public Iterator<Item> iterator() {
// return an iterator over items in order from front to end
return new DequeIterator<Item>();
}
private class DequeIterator<Item> implements Iterator<Item> {
private Node current = first;
public boolean hasNext() {
return current.next != null;
}
public Item next() {
Item item = current.value;
current = current.next;
return item;
}
public void remove() {}
}
public static void main(String[] args) {
// unit testing
Deque<Integer> dq = new Deque<Integer>();
}
}
In the outer scope :
public class Deque<Item> implements Iterable<Item> {
is used.
And in the inner scope:
private class DequeIterator<Item> implements Iterator<Item> {
is used.
In the scope of DequeIterator
. I expected the local-scope (inner-class-scope) Item
will shadow the class-scope Item
from Deque<Item>
.
However, during compiling stage, javac
will throw an error like this:
Deque.java:2: error: incompatible types
Item item = current.value;
^
required: Item#2
found: Item#1
where Item#1,Item#2 are type-variables:
Item#1 extends Object declared in class Deque
Item#2 extends Object declared in class Deque.DequeIterator
It says Item#2
and Item#1
is incompatible types, which looks quite confusing to me because I have passed the type parameter Item
into DequeIterator
using new DequeIterator<Item>()
.
Does anyone have any idea about this?