尋找一個遞歸的手,我知道這是一個簡單的問題,但在某處退出但不知道如何/在哪裏!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