程序中的 StackOverflow 错误,我找不到原因

void recur(int i)

{

    if(i==n)

        return;

    String sub="";

    for(int j=i+1;j<n;j++)

    {

        sub=s.substring(i,j);

        if(isPalindrome(sub))

            System.out.println(sub);

    }

    recur(i++);

}

我StackOverflowError在


sub=s.substring(I,j); 

声明。


s="geeks", initial value of I=0;


MMTTMM
浏览 171回答 2
2回答

噜噜哒

尝试这个public class P {&nbsp; &nbsp; public static final String s="geeks";&nbsp; &nbsp; static void recur(int i){&nbsp; &nbsp; &nbsp; &nbsp; int n=6;&nbsp; &nbsp;//Size of string&nbsp; &nbsp; &nbsp; &nbsp; if(i==n)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; String sub="";&nbsp; &nbsp; &nbsp; &nbsp; for(int j=i+1;j<n;j++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sub=s.substring(i,j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Any Function&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(sub);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; recur(++i);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; P.recur(0);&nbsp; &nbsp; }}

Smart猫小萌

recur(i++);表达式i++的值为i当前时间的值;然后你增加它。因此,您基本上是在调用:recur(i); i++;所以你只是recur用相同的参数再次调用。尝试:recur(++i);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java