寻找一个递归的手,我知道这是一个简单的问题,但在某处退出但不知道如何/在哪里!Java - 递归地统计列表中的单词的出现
这里是我的递归方法:
public static int getNumAppearances(myList<String> l, String word)
{
int index = 0;
int count = 0;
String search = word;
if(index > l.my_get_length()-1)
{
return 0;
}
else if(l.my_get_element(index).equals(search))
{
count++;
index++;
}
return count + getNumAppearances(l, word);
}
干杯!
编辑,myList中类:
public interface myList<T> {
//-------------------------------------------------------------------
// Create an empty MyList: create_empty
//-------------------------------------------------------------------
//public myList create_empty(); --> Java does not support constructors in interfaces
//-------------------------------------------------------------------
// Basic Operation --> Get number of elements in MyList: my_get_length
//-------------------------------------------------------------------
public int my_get_length();
//-------------------------------------------------------------------
// Basic Operation --> Get element at of MyList at a concrete position: my_get_element
//-------------------------------------------------------------------
public T my_get_element(int index) throws myException;
//-------------------------------------------------------------------
// Basic Operation --> Add element to MyList at a concrete position: my_add_element
//-------------------------------------------------------------------
public void my_add_element(int index, T element) throws myException;
//-------------------------------------------------------------------
// Basic Operation --> Remove element of MyList at a concrete position: my_remove_element
//-------------------------------------------------------------------
public void my_remove_element(int index) throws myException;
}
我意识到你需要的理想传递给方法的指数但不幸的是,这不是他有它设置方式!
请注明您所遇到的问题。 –
递归执行此操作没有理由。只需循环查看您的列表并查看单词。 – AndyB
@AndyB很可能这是一个家庭作业任务,旨在教授递归 - 因此不允许使用更明智的方法 – Catchwa