我试图创建一个基于引用的排序列表,但每次我尝试编译它时,都会收到此错误。
"缺少退货声明"
我真的不明白为什么我会得到它,虽然这似乎是我的locateIndex方法的一个问题
这是我的代码
public class SortedListReferenceBased extends ListReferenceBased
{
public int index;
public int numItems;
public Node head;
public SortedListReferenceBased()
// creates an empty list
{
numItems = 0;
head = null;
} // end default constructor
public void add(String item) throws ListException
// Inserts item into its proper position in a sorted list
// Throws an exception if the item connot be placed on the list
{
try
{
locateIndexToAdd(item);
super.add(index,item);
}
catch(Exception e)
{
throw new ListException("Add to List failed: " + e.toString());
}
}
public void remove(String item) throws ListException
// Removes the item from a sorted list.
// Throws an exception if the item is not found.
{
try
{
locateIndexToRemove(item);
super.remove(index);
// YOUR CODE WILL BE HERE...
// REQUIREMENT: USE "locateIndex(String item)" method.
}
catch(Exception e)
{
throw new ListException("Remove " + item.toString() + " from List failed: " + e.toString());
}
}
public int locateIndexToAdd(String item)
// Returns the position where the item belongs or exists in a sorted list;
// item and the list are unchanged.
{
int stop = 0;
index = 0;
while (index <= numItems && stop == 0)
{
Object curr = super.get(index);
String current = curr.toString();
if (item.compareTo(current) > 0)
index++;
else
{
stop = 1;
return index;
}
}
}
public int locateIndexToRemove(String item)
// Returns the position where the item belongs or exists in a sorted list;
// item and the list are unchanged.
{
int found = 0;
index = 0;
while (index <= numItems && found == 0)
{
Object curr = super.get(index);
String current = curr.toString();
if (item.compareTo(current) == 0)
{
found = 1;
return index;
}
else
{
index ++;
}
}
if (found == 0)
System.out.println("Item not found");
}
} // end SortedListReferenceBased
答案 0 :(得分:2)
此方法确实缺少return
语句
public int locateIndexToRemove(String item)
// Returns the position where the item belongs or exists in a sorted list;
// item and the list are unchanged.
{
int found = 0;
index = 0;
while (index <= numItems && found == 0)
{
Object curr = super.get(index);
String current = curr.toString();
if (item.compareTo(current) == 0)
{
found = 1;
return index;
}
else
{
return index ++; // or here
}
}
if (found == 0)
System.out.println("Item not found");
return index; // <-- the missing return statement.
}
这里有一个:
if (item.compareTo(current) == 0)
{
found = 1;
return index;
}
是不够的,因为如果if
语句返回false会发生什么,则不再有值返回,因此编译器会抱怨缺少return语句。
答案 1 :(得分:0)
WonderWorld回答得对。 当你在locateIndexToRemove方法签名中指定你的返回类型必须是int时,你必须返回int。 在这个方法中发生的事情是你的locateIndexToRemove的while循环中的if和else语句没有运行,或者你的while本身没有运行,这使得locateIndexToRemove方法无法返回一个int值。
答案 2 :(得分:0)
我认为这会为你效劳,
int stop = 0;
index = 0;
while (index < numItems && stop == 0)
{
Object curr = super.get(index);
String current = curr.toString();
if (item.compareTo(current) > 0)
index++;
else
{
stop = 1;
}
}
return index;
我认为你必须增加和减少&#34; numItems&#34;添加和删除项目时。