2014-03-25 145 views
0

我不能找到這個練習的解決方案,這裏是任務:遞歸helper方法


(數組中的指定字符的出現)編寫 認定的出現的次數遞歸方法數組中的指定字符。 您需要 定義以下兩種方法。第二個是遞歸輔助方法。

公共靜態詮釋計數(CHAR []字符,字符CH)

公共靜態詮釋計數(燒焦[]字符,字符CH,INT高)

編寫提示用戶的測試程序在一行中輸入一個字符列表, 和一個字符,並在列表中顯示該字符的出現次數。


1)我能解決這個問題只有當我添加另一個參數(INT指數),但我怎麼能做到這一點無需增加其他參數或使用循環?

2)爲什麼輔助方法在那裏?我不明白遞歸中輔助方法的目的。

這裏是我的解決方案:

package occurencesinarray; 

import java.util.Scanner; 

public class Start { 
public static void main(String[] args){ 
    System.out.println("Enter few characters: "); 
    Scanner scan = new Scanner(System.in); 
    String s = scan.nextLine(); 
    char[] chars = new char[s.length()]; 
    for(int i = 0; i < s.length(); i++){ 
     chars[i] = s.charAt(i); 
    } 
    System.out.println("Enter desired character: "); 
    char ch = scan.nextLine().charAt(0); 

    System.out.println(count(chars, ch)); 
} 

public static int count(char[] chars, char ch){ 
    return count(chars, ch, 0, 0); 
} 

public static int count(char[] chars, char ch, int high, int index){ 
    if(index == chars.length){ 
     return high; 
    } 
    if(chars[index] == ch){ 
     return count(chars, ch, high + 1, index + 1); 
    } else{ 
     return count(chars, ch, high, index + 1); 
    } 
} 
} 

回答

1

正如AllenKll已經指出的那樣,high值應該大概需要,你打算爲你的index的作用。您一直在統計high變量中發生的次數,但是這種計數可以在遞歸中「隱藏」。

這些用於遞歸的「助手」方法的目的一般是這樣的:它們通常具有(至少)一個額外的參數,以某種方式描述遞歸已經進行了多久,或者它還有多遠才能繼續。至於後者的例子:你也可以使用了high變量作爲「倒計時」,通過寫

public static int count(char[] chars, char ch) 
{ 
    return count(chars, ch, chars.length - 1); 
} 

public static int count(char[] chars, char ch, int high) 
{ 
    if (high == -1) 
    { 
     return 0; 
    } 
    if (chars[high] == ch) 
    { 
     return 1 + count(chars, ch, high - 1); 
    } 
    return count(chars, ch, high - 1); 
} 

當然,一個只能提供輔助方法。與其說

count(chars, ch); 

的,你可以要求用戶呼叫

count(chars, ch, 0); 

但這裏的問題是,這種方法可能會被誤用:當那麼用戶傳遞一個錯誤的值作爲最後一個參數,那麼方法不起作用。

注意:這整個「輔助方法」的東西只有當幫助方法是私人有意義。當它是public時,用戶仍可能調用錯誤的方法。我看到public修飾符在任務描述中被要求,但是......當你讓你的教師知道這個缺陷時,你可能會收到一些獎勵點數;-)

1

如何:

高代表

public static int count(char[] chars, char ch, int high){ 
    if(high == chars.length){ 
     return 0; 
    } 
    if(chars[index] == ch){ 
     return count(chars, ch, high + 1) + 1; 
    } else{ 
     return count(chars, ch, high + 1); 
    } 
} 

輔助方法是這樣調用者不需要了解索引「高」參數。在另一種語言中,如C可以有默認參數,則不需要。

1

首先,您的幫助(遞歸)方法應該是private,而不是public。它需要知道如何工作才能正確調用它。這與良好的軟件設計背道而馳,它說實施並不重要,只要它的合同得到遵守。

第一種方法是設置遞歸方法的初始條件(參數)的面向公衆的外觀。真正的行動是在遞歸方法。

遞歸(輔助)方法通常具有必須被確定(和編碼)三兩件事:

  • 初始狀態
  • 終止條件
  • 如何前進到下一個狀態

初始狀態通常由facade方法處理,就像你的情況一樣。

端接狀態通常是在該方法中代碼的第一行,並導致立即返回(也爲你的情況的情況下)

如果終止條件沒有被滿足時,狀態(和/或計算)可以保存在本地以貢獻返回值,然後該方法使用將狀態推進到下一個位置的參數來調用它自己。自我呼叫的結果被返回,可能與保存狀態的數據相結合。

在你的情況下,你將本地狀態傳遞給下一個呼叫。不要這樣做。相反,結合

public static int count(char[] chars, char ch, int index) { 
    // Test for terminating condition 
    if (index == chars.length) { 
     return 0; 
    } 
    // return 1 if ch matches (0 otherwise) plus count from the remaining input 
    return (chars[index] == ch ? 1 : 0) + count(chars, ch, index + 1); 
} 

而且隨着0指數調用它來啓動進程。

0
public static int count(char[] chars, char ch) 
{ 
    return count(chars, ch, 0); 
} 

public static int count(char[] chars, char ch, int high) 
{ 
    int count = high; 
    if chars.size()== 0 
    { 
     return count; 
    } 
    else if(chars.indexOf(0) == ch) 
    { 
     count++; 
    } 

    return count(Arrays.copyOfRange(charList, 1, charList.size()), ch, count); 
}