Java中的递归方法

Java中的递归方法,java,Java,我试图使用递归方法将字符串更改为字符数组,但遇到了一个错误 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(Unknown Source) 我只想使用递归方法(而不是循环或toChar方法)来解决这个问题 当str的长度为0时会发生什么情况 public class Recur

我试图使用递归方法将字符串更改为字符数组,但遇到了一个错误

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(Unknown Source)
我只想使用递归方法(而不是循环或toChar方法)来解决这个问题

str
的长度为0时会发生什么情况

public class Recur
{
    private static char [] stringToChar (
        String str, char [] destination, int offset)
    {
        if (destination == null)
        {
            destination = new char [str.length ()];
            offset = 0;
        }

        if (offset < str.length ())
        {
            destination [offset] = str.charAt (offset);
            stringToChar (str, destination, offset + 1);
        }
        return destination;
    }

    public static void main (String [] args)
    {
        char [] chars = stringToChar ("this is a test", null, 0);

        for (char c: chars)
            System.out.println (c);
    }
}
str
的长度为0时会发生什么情况?

公共类重复出现
public class Recur
{
    private static char [] stringToChar (
        String str, char [] destination, int offset)
    {
        if (destination == null)
        {
            destination = new char [str.length ()];
            offset = 0;
        }

        if (offset < str.length ())
        {
            destination [offset] = str.charAt (offset);
            stringToChar (str, destination, offset + 1);
        }
        return destination;
    }

    public static void main (String [] args)
    {
        char [] chars = stringToChar ("this is a test", null, 0);

        for (char c: chars)
            System.out.println (c);
    }
}
{ 私有静态字符[]stringToChar( 字符串str,char[]目标,int偏移量) { 如果(目标==null) { destination=新字符[str.length()]; 偏移量=0; } 如果(偏移量
公共类重复出现
{
私有静态字符[]stringToChar(
字符串str,char[]目标,int偏移量)
{
如果(目标==null)
{
destination=新字符[str.length()];
偏移量=0;
}
如果(偏移量
为什么要重新创建
字符串#toCharArray
,特别是为什么要使用递归进行重新创建?@T.J.Crowder学习目的。您的基本情况也会调用递归函数,而不是停止。与此类似@buni:这个问题特别不适合用递归解决。如果您想了解递归,我建议您执行另一项任务,例如(比如)遍历树结构。问题是递归任务必须以增量的方式建立其结果,这对于
char[]
返回类型很难做到。为什么您要尝试重新创建
String\tocharray
,尤其是,为什么要用递归重新发明它?@T.J.Crowder学习目的。您的基本案例也会调用递归函数,而不是停止。与此类似@buni:这个问题特别不适合用递归解决。如果您想了解递归,我建议您执行另一项任务,例如(比如)遍历树结构。问题在于递归任务必须以增量的方式建立其结果,这对于
char[]
返回类型是很困难的。
public class Recur
{
    private static char [] stringToChar (
        String str, char [] destination, int offset)
    {
        if (destination == null)
        {
            destination = new char [str.length ()];
            offset = 0;
        }

        if (offset < str.length ())
        {
            destination [offset] = str.charAt (offset);
            stringToChar (str, destination, offset + 1);
        }
        return destination;
    }

    public static void main (String [] args)
    {
        char [] chars = stringToChar ("this is a test", null, 0);

        for (char c: chars)
            System.out.println (c);
    }
}