前端参数无法转为后端实体内部类_内部类内部的静态类不能转换

Inspired in this question: How to implements Iterable I decided to make a basic linked list implementation and implement an iterator in order to have a code like this:

MyList myList = new MyList();

myList.add("hello");

myList.add("world");

for(String s : myList) {

System.out.println(s);

}

The code wasn't hard to deal with, creating a class MyList implements Iterable with a private static class Node and a private class MyListIterator implements Iterator, but then I came across a problem when implementing my own version of Iterator#remove:

class MyList implements Iterable {

private static class Node {

//basic node implementation...

}

private Node head;

private Node tail;

//constructor, add methods...

private class MyListIterator implements Iterator {

private Node headItr;

private Node prevItr;

public MyListIterator(Node 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 to

//another.main.MyList.Node"

head = headItr.getNext();

//line below doesn't compile, just for testing purposes (it will be deleted)

head = headItr;

}

}

}

}

This error message raised my curiosity. I was looking on the net about this problem but found nothing (or probably I'm not so good at searching this kind of issues). What would be the reason of two variables from the same type being compared but not being assignable to each other?

By the way, I know that I can just look at the code of LinkedList and check how the Java designers implemented this and copy/paste/adapt it to my own implementation, but I prefer to have an explanation and understanding of the real problem.

Complete code that shows my current implementation of MyList class:

class MyList implements Iterable {

private static class Node {

private T data;

private Node next;

public Node(T data) {

super();

this.data = data;

}

public T getData() {

return data;

}

public Node getNext() {

return next;

}

public void setNext(Node next) {

this.next = next;

}

}

private Node head;

private Node tail;

private int size;

public MyList() {

head = null;

tail = null;

}

public void add(T data) {

Node node = new Node(data);

if (head == null) {

head = node;

tail = head;

} else {

tail.setNext(node);

tail = node;

}

size++;

}

private class MyListIterator implements Iterator {

private Node headItr;

private Node prevItr;

public MyListIterator(Node 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 iterator() {

return new MyListIterator(head);

}

}

解决方案

This is the problem:

class MyList implements Iterable {

private class MyListIterator implements Iterator {

...

}

}

(It doesn't help that in your cut down version you've made MyList non-generic.)

At that point there are two different T type variables - the one in the nested class and the one in the outer class. You don't need Node to be generic - you just need:

class MyList implements Iterable {

private class MyListIterator implements Iterator {

...

}

}

Now there's only one T - the one in the outer class. It's not like you want the list iterator to have a different T from the one declared in the enclosing instance, so you don't want it to be generic.

To put it another way: try making MyListIterator generic in a type parameter with a different name, and then it'll be clearer what's going wrong, as the two names will be distinguishable in the error message. It's effectively:

Type mismatch: cannot convert from another.main.MyList.Node to

another.main.MyList.Node

(or vice versa).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值