这是我的代码:
package datastructures;
import java.util.Iterator;
public class Stack<T>{
private class Node<T>{
T data;
Node next;
}
private int size;
private Node head;
private Node newNode(T data){
Node new_node = new Node();
new_node.data = data;
new_node.next = null;
return new_node;
}
public Stack(){
size = 0;
head = null;
}
public T pop() {
if(head == null)
return null;
T ret_val = head.data; //Error here
head = head.next;
this.size--;
return ret_val;
}
}
我在 pop 方法中收到错误,错误如下:
Error: incompatible types : java.lang.Object cannot be converted to T
我不明白这个错误,我没有在代码中的任何地方使用对象。
当您将 Node 声明为
private class Node<T>
您正在声明一个泛型类型,其中另一个 T 作为封闭类型中的 T。你隐藏了 T。所以,简而言之,就好像你将其声明为
private class Node<E>
Just use
private class Node
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)