简体   繁体   English

如何递归打印字符串中的每个字符 function

[英]How to recursively print out each character one at a time in a String function

I am trying to get better at understanding recursive features through the usage of String functions.我试图通过使用字符串函数来更好地理解递归特性。 I am trying to create a function that recursively prints out one char at a time until the stack reaches the end of the String.我正在尝试创建一个 function 递归地一次打印一个字符,直到堆栈到达字符串的末尾。 For example the input would look like this:例如,输入看起来像这样:

String str = "Hi";
//Output:
H
i

I have been practicing recursion using Int functions, and have been slowly trying to ease my way into understanding recursion using String functions.我一直在练习使用 Int 函数进行递归,并且一直在慢慢尝试使用 String 函数来简化理解递归的方式。 I am confused on how exactly to call the function to increase or decrease.我对如何准确调用 function 来增加或减少感到困惑。 With Int functions it's easy to do using this methodology.使用 Int 函数很容易使用这种方法。

int function(int N){
    return function(N+1);
}

Here is the current code I have.这是我拥有的当前代码。

String str = "Hi";

public String forward(String str){
    if(str.equals(null) || str.length()<2) return str;

    System.out.println(str.charAt(0));
    return forward(str.substring(0, str.length()-1));
}

The expected output should be:预期的 output 应该是:

H
i

But instead the current output is:但是当前的 output 是:

H
H

Thank you for your time and patience, any assistance or guidance is greatly appreciated.感谢您的时间和耐心,非常感谢任何帮助或指导。

Because you are recursing with the same String every time.因为您每次都使用相同的String进行递归。 You want to actually skip the first character.您实际上想跳过第一个字符。 And you don't need to return anything.而且你不需要返回任何东西。 And the method should be static .方法应该是static And don't use Object.equals(null) - that would give you a NullPointerException on null .并且不要使用Object.equals(null) - 这会给你一个NullPointerExceptionnull Something like,就像是,

public static void forward(String str) {
    if (str == null || str.isEmpty()) {
        return;
    }
    System.out.println(str.charAt(0));
    forward(str.substring(1));
}

Outputs输出

H
i

In order to create a recursive method, you have to implement two crucial parts of recursive logic: BASE CASE and RECURSIVE CASE .为了创建递归方法,您必须实现递归逻辑的两个关键部分: BASE CASERECURSIVE CASE

Base case - is represented by the input for which result is known in advance.基本情况 - 由预先知道结果的输入表示。 In this case, it's the end of the string.在这种情况下,它是字符串的结尾。

Recursive case - is where your logic resides, and where recursive calls are made.递归案例 - 是您的逻辑所在的位置,以及进行递归调用的位置。

    public static void main(String[] args) {
        printStr("test");
    }

    public static void printStr(String str) {
        if (str.isEmpty()) return;
        helper(str, 0);
    }

    private static void helper(String str, int i) {
        if (i >= str.length()) return;
        System.out.println(str.charAt(i));
        helper(str, i + 1);
    }

A recursive function calls itself from within the same function.递归 function 从同一个 function 中调用自身。 The function needs to have a way to escape eventually or there would be a StackOverFlowError. function 最终需要有办法逃脱,否则会出现 StackOverFlowError。

public static void main(String[] args) {
  printStr("Hi");
}

private static String printStr(String s) {
  if (s == null || s.length() == 0) {
    return null;
  }
  System.out.println(s.charAt(0));
  return s.length() >= 1 ? printStr(s.substring(1)) : null;
}

in your code: 1.first cycle:str is "Hi";then print "H";then str.substring(0, str.length()-1) is H 2.second cycle:str is "H";then return and end the recursive and do nothing在您的代码中: 1.first cycle:str is "Hi";then print "H";then str.substring(0, str.length()-1) is H 2.second cycle:str is "H";then返回并结束递归并且什么都不做

fix your code:修复你的代码:

   public static String forward(String str) {
    if (str.length() == 0) {
        return null;
    }
    System.out.println(str.charAt(0));
    return forward(str.substring(1));
}

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

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