【发布时间】:2015-10-02 13:55:36
【问题描述】:
我正在使用我以前实验室中关于链表的旧代码编写一个 pop 方法。我的代码看起来很完美,但我不断收到错误
错误:类型不兼容
返回 top.data;
必需:AnyType
找到:对象
其中 AnyType 是一个类型变量:
AnyType 扩展了在类堆栈中清除的对象
我不知道如何解决这个错误!我在其他情况下阅读了有关此相同错误的其他帖子,但已知似乎适用于我的情况,或者我不知道如何将其应用于我的情况。
public class Stack<AnyType> implements StackInter<AnyType>
{
MyNode top = new MyNode();
public void push(AnyType x)
{
MyNode newNode = new MyNode();
newNode.data = top.data;
top.data = x;
newNode.next = top.next;
top.next = newNode;
}
public AnyType pop()
{
if(isEmpty())
return null;
else{
AnyType topStuf = top.data; // Getting error on this line
top = top.next;
return topStuff;
}
}
public boolean isEmpty()
{
return top == null;
}
public AnyType(peek)
{
return top.data;
}
}
public class MyNode<AnyType>
{
public AnyType data;
public MyNode<AnyType> next;
}
【问题讨论】:
标签: linked-list stack