我无法找到适合此练习的解决方案,这是任务:
(数组中指定字符的出现)编写一个递归方法 查找数组中指定字符的出现次数。 你需要 定义以下两种方法。第二种方法是递归辅助方法。
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);
}
}
}
答案 0 :(得分: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);
}
}
帮助方法是让呼叫者不需要知道&#34;高&#34;参数。在另一种语言中,例如C,你可以拥有默认参数,这是不必要的。
答案 1 :(得分: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
调用它来启动该过程。
答案 2 :(得分: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
修饰符,但是......当你让讲师意识到这个缺陷时,你可能会收到一些奖励积分; - )
答案 3 :(得分: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);
}