Lui*_*oza 8 java generics static inner-classes
灵感来自这个问题:如何实现Iterable我决定创建一个基本的链表实现并实现一个迭代器,以便得到这样的代码:
MyList<String> myList = new MyList<String>();
myList.add("hello");
myList.add("world");
for(String s : myList) {
System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)
代码并不难处理,class MyList<T> implements Iterable<T>用a private static class Node<T>和a 创建一个private class MyListIterator<T> implements Iterator<T>,但是在实现我自己的版本时我遇到了一个问题Iterator#remove:
class MyList<T> implements Iterable<T> {
private static class Node<T> {
//basic node implementation...
}
private Node<T> head;
private Node<T> tail;
//constructor, add methods...
private class MyListIterator<T> implements Iterator<T> {
private Node<T> headItr;
private Node<T> prevItr;
public MyListIterator(Node<T> headItr) {
this.headItr = headItr;
}
@Override
public void remove() {
//line below compiles
if (head == headItr) {
//line below compiles
head = head.getNext();
//line below doesn't and gives me the message
//"Type mismatch: cannot convert from another.main.MyList.Node<T> to
//another.main.MyList.Node<T>"
head = headItr.getNext();
//line below doesn't compile, just for testing purposes (it will be deleted)
head = headItr;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
此错误消息引起了我的好奇心.我正在网上看到这个问题,但没有发现(或者我可能不太擅长搜索这类问题).比较相同类型的两个变量但不能彼此分配的原因是什么?
顺便说一句,我知道我可以查看代码LinkedList并检查Java设计者如何实现它并将其复制/粘贴/调整到我自己的实现,但我更喜欢对真正的问题进行解释和理解.
显示我当前MyList类实现的完整代码:
class MyList<T> implements Iterable<T> {
private static class Node<T> {
private T data;
private Node<T> next;
public Node(T data) {
super();
this.data = data;
}
public T getData() {
return data;
}
public Node<T> getNext() {
return next;
}
public void setNext(Node<T> next) {
this.next = next;
}
}
private Node<T> head;
private Node<T> tail;
private int size;
public MyList() {
head = null;
tail = null;
}
public void add(T data) {
Node<T> node = new Node<T>(data);
if (head == null) {
head = node;
tail = head;
} else {
tail.setNext(node);
tail = node;
}
size++;
}
private class MyListIterator<T> implements Iterator<T> {
private Node<T> headItr;
private Node<T> prevItr;
public MyListIterator(Node<T> headItr) {
this.headItr = headItr;
}
@Override
public boolean hasNext() {
return (headItr.getNext() != null);
}
@Override
public T next() {
T data = headItr.getData();
prevItr = headItr;
if (hasNext()) {
headItr = headItr.getNext();
}
return data;
}
@Override
public void remove() {
if (head == headItr) {
//problem here
head = headItr.getNext();
}
//implementation still under development...
}
}
@Override
public Iterator<T> iterator() {
return new MyListIterator<T>(head);
}
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 16
这就是问题:
class MyList<T> implements Iterable<T> {
private class MyListIterator<T> implements Iterator<T> {
...
}
}
Run Code Online (Sandbox Code Playgroud)
(在您的减少版本中,您制作MyList非通用版本没有帮助.)
此时有两个不同的T类型变量 - 嵌套类中的变量和外部类中的变量.您不需要Node通用 - 您只需要:
class MyList<T> implements Iterable<T> {
private class MyListIterator implements Iterator<T> {
...
}
}
Run Code Online (Sandbox Code Playgroud)
现在只有一个 T - 外层的一个.它不像你希望列表迭代器与封闭实例中声明的那个不同 T,所以你不希望它是通用的.
换句话说:尝试MyListIterator在具有不同名称的类型参数中创建泛型,然后它会更清楚出错的原因,因为这两个名称在错误消息中是可区分的.这是有效的:
Type mismatch: cannot convert from another.main.MyList.Node<TOuter> to
another.main.MyList.Node<TInner>
Run Code Online (Sandbox Code Playgroud)
(或相反亦然).
迭代器应声明为
private class MyListIterator implements Iterator<T>
Run Code Online (Sandbox Code Playgroud)
而不是
private class MyListIterator<T> implements Iterator<T>
Run Code Online (Sandbox Code Playgroud)
通过将其声明为MyListIterator,其泛型类型T与其封闭类中的T不同.
从声明中删除type参数MyListIterator:
private class MyListIterator implements Iterator<T>
Run Code Online (Sandbox Code Playgroud)
和电话 iterator()
public Iterator<T> iterator() {
return new MyListIterator(head);
}
Run Code Online (Sandbox Code Playgroud)
在您当前的版本中,T of MyListIterator与T中的T不同MyList.