简体   繁体   English

辅助方法:将字符串中的字符加倍,但在第一个字符后停止递归

[英]Helper Method: Doubling Chars in a String but stops recursion after first char

This is a homework assignment that I got stuck on. 这是我的一项家庭作业。 I am giving an ArrayList and I need to return a String with all the characters doubled. 我给一个ArrayList,我需要返回一个字符串,所有字符加倍。 Input: abc, Output:aabbcc. 输入:abc,输出:aabbcc。 What I'm getting as my output is aa, then it stops. 我得到的输出是aa,然后停止。 I've done this multiple times with a loop but I can't seem to get it with recursion. 我已经循环执行了多次,但是我似乎无法通过递归得到它。

I tried re-editing my code and also tried using a counter but I either get a null return or I'll get an overstack flow error. 我尝试重新编辑我的代码,也尝试使用计数器,但是我得到的返回为null或得到堆栈溢出错误。 I went to Oracles website to figure out other String methods I could use and tried changing my code with other methods but still no luck. 我去了Oracle网站,找出了我可以使用的其他String方法,并尝试用其他方法更改我的代码,但还是没有运气。

My other methods work of turning the ArrayList to a String but it's my last method I'm having troubles with. 我的其他方法可以将ArrayList转换为字符串,但这是我遇到麻烦的最后一个方法。

public String dupEachChar2(String str) {
    //str is going to be a string with abc in it.
    String x = "";
    int count = 0;

    // I'm checking if there is only 1 char, if so just return it twice to 1 string.
    if(str.length() == 1) {
        return str + str;
    } else if(count != str.length()) {
        //Doubling part that I can not figure out.
        x += str.substring(count, count+1) + str.substring(count, count+1);
        count++;
        dupEachChar2(str.substring(count));
    }
    return x;
}

You can use the back reference feature of String.replaceAll(). 您可以使用String.replaceAll()的向后引用功能。 To replace each character of a string with duplicates do the following: 要将字符串的每个字符替换为重复项,请执行以下操作:

String a = "abc";
String b = a.replaceAll("(.)", "$1$1");
b is now equals to aabbcc

Here is how this works. 这是这样的。

  • . means match any character and the () are a group capture for the regular expression. 表示匹配任何字符,并且()是正则表达式的组捕获。
  • $1 means get what was matched in the first (ie #1) group. $1表示获得第一个(即#1)组中匹配的内容。 Then simply use it twice to double the character. 然后只需使用它两次即可使字符加倍。

try this 尝试这个

public String dupEachChar2(String str) {

        if(str.length() == 1) 
            return str + str;
        else 
            return str.substring(0,1)+str.substring(0,1)+dupEachChar2(str.substring(1));
    }
}

the issue with your code is you're treating the variable x (and count) as if it is shared between every instance of the method, when in fact it is a new variable for every time that method is called. 代码的问题是,您将变量x(和计数)视为在方法的每个实例之间共享,实际上,每次调用该方法时它都是一个新变量。 Besides, you don't need the variable x here because you intended for it to hold the entire new string, but you already have that from the value returned by the prior iteration of your recursive method. 此外,这里不需要变量x,因为您打算让它保存整个新字符串,但是您已经从递归方法的先前迭代返回的值中获得了变量x。 As for count, you can just look at how many characters are left in the string, (chopping one off every time) and stopping when they reach 1. 至于计数,您只需看一下字符串中还剩下多少个字符(每次都切掉一个)并在达到1时停止。

If you were to put in the minimal amount of effort to make your code function correctly, I would say pass x in as a second parameter, and use "" when you first call it from wherever. 如果您要花最少的精力使代码正确运行,我会说将x作为第二个参数传递,并在从任何地方首次调用时使用“”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM