首页 文章

递归辅助方法

提问于
浏览
0

我无法找到适合此练习的解决方案,这是任务:


(在数组中出现指定的字符)编写一个递归方法,查找数组中指定字符的出现次数 . You need to define the following two methods. 第二个是递归辅助方法 .

public static int count(char [] chars,char ch)

public static int count(char [] chars,char ch,int high)

编写一个测试程序,提示用户输入一行中的字符列表和一个字符,并显示列表中字符的出现次数 .


1)我只有在添加另一个参数(int index)时才能解决它,但是如何在不添加其他参数或使用for循环的情况下执行此操作?

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);
    }
}
}

4 回答

  • 1

    正如AllenKll已经指出的那样, high 值可能应该扮演你想要的角色 index . 您一直在计算 high 变量中的出现次数,但这种计数在递归中可以是"hidden" .

    一般来说,这些递归方法的目的正是这样:它们通常具有(至少)一个附加参数,以某种方式描述递归已经进行了多远或者它仍然有多远 . 作为后者的一个例子:您也可以通过写作将 high 变量用作"countdown"

    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);
    

    但是这里的问题是这个方法可能被滥用:当用户将错误的值作为最后一个参数传递时,该方法将不起作用 .

    注意:当辅助方法是 private 时,整个"helper method"事物才有意义 . 当它是 public 时,用户仍可能调用错误的方法 . 我看到在任务描述中请求了 public 修饰符,但是......当你让讲师意识到这个缺陷时,你可能会收到一些奖励积分;-)

  • 1

    这个怎么样:

    high代表索引

    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,您可以使用默认参数,这是没有必要的 .

  • 0

    首先,你的助手(递归)方法应该是 private ,而不是 public . 它需要知道它是如何工作才能正确调用它 . 这违背了良好的软件设计,即只要遵守 Contract ,实施并不重要 .

    第一种方法只是面向公众的外观,它设置递归方法的初始条件(参数) . 实际操作在递归方法中 .

    递归(辅助)方法通常有三件事必须确定(和编码):

    • 初始状态

    • 终止条件

    • 如何前进到下一个州

    初始状态通常由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 调用它来启动该过程 .

  • 1
    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);
    }
    

相关问题