该方法应该从给定索引的链接列表中返回一个类型为" type“的节点。
public type get(int index) throws Exception
{
int currPos = 0;
Node<type> curr = null;
if(start!= null && index >=0)
{
curr = start;
while(currPos != index && curr != null)
{
curr = curr.getNext();
currPos++;
}
}
return curr;为什么它在编译时给我一个“不兼容类型”错误?
发布于 2013-10-16 15:54:47
您已经声明了返回type对象的方法,但是您正在尝试返回被声明为Node<type>的curr。Node类可能有一个getValue()方法(或类似的方法)来检索存储在节点中的type对象。您应该将最后一行更改为:
return curr.getValue();更好的是,因为此时curr有可能成为null:
return curr == null ? null : curr.getValue();https://stackoverflow.com/questions/19408031
复制相似问题