递归方法中的java.lang.StackOverflowerr

递归方法中的java.lang.StackOverflowerr,java,recursion,permutation,Java,Recursion,Permutation,我目前正在研究一种算法,用n个整数序列输出所有排列,该函数在6个元素之前运行良好,但当超过该值时,我得到了StackOverflowerError消息。我已经读过一些关于这个主题的问题,但是我发现当递归方法有无限多的调用时就会发生这种情况,而当它的基本情况执行时,情况似乎不是这样 //int[] c parameter receives an array with n elemens to permute //int i represents the length of the sequenc

我目前正在研究一种算法,用n个整数序列输出所有排列,该函数在6个元素之前运行良好,但当超过该值时,我得到了StackOverflowerError消息。我已经读过一些关于这个主题的问题,但是我发现当递归方法有无限多的调用时就会发生这种情况,而当它的基本情况执行时,情况似乎不是这样

//int[] c parameter receives an array with n elemens to permute 
//int i represents the length of the sequence to permute (ai,...,aN)
public boolean isPermutated(int[] c, int i)
{
    int max = 0; // the maximum number from a sequence (a1,...,aN) and 
    int index = 0; // the index where the maximum is
    int lessThan; // an index preceded by maximum
    //Base case
    if(i == 1)
    {
        return false;
    }        
    // Gives the Maximum element from a sequence (ai,...,aN) 
    for(int count = c.length - i; count < c.length; count++)
    { 
       if(max < c[count])
       {
           max = c[count];
           index = count;
       }   
    }
    // Swap the Maximum from index to index-1
    if(max != c[c.length - i])
    {  
        lessThan = c[index-1];
        c[index-1] = max;
        c[index] = lessThan;
        //move each maximum from a sequence (a,...,aN) to the last index ex, (3,2,1)->(2,1,3)
        if(i != c.length)
        {
            while((i-c.length) != 0)
            {
                for(int count  = c.length - (i+1); count < c.length-1; count++)
                {
                    int before;
                    int after;
                    before = c[count];
                    after = c[count+1];
                    c[count] = after;
                    c[count+1] = before;     
                } 
                i++;
            }
        }

        // print array c elements    
        for(int e:c)
            System.out.print(e);
        System.out.println();
        isPermutated(c, c.length);     
    } 
    else 
    {
        i--;
        isPermutated(c, i);        
    }
    return true;
}
//int[]c参数接收一个数组,其中有n个元素要排列
//int i表示要排列的序列的长度(ai,…,aN)
公共布尔值isPermutated(int[]c,int i)
{
int max=0;//序列(a1,…,aN)和
int index=0;//最大值为
int lessThan;//前面有最大值的索引
//基本情况
如果(i==1)
{
返回false;
}        
//给出序列中的最大元素(ai,…,aN)
for(int count=c.length-i;count(2,1,3)
如果(i!=c.长度)
{
而((i-c.长度)!=0)
{
对于(int count=c.length-(i+1);count

我只是很沮丧,因为我需要一个包含10个元素的数组。是否有任何具有相同方法签名的伪代码,或者是否有任何方法调整堆栈跟踪,以部分解决问题?

您的代码是否遗漏了返回语句?为什么调用isPermutated而不担心返回的值


您的代码永远不会返回true。

您的代码不会错过返回语句吗?为什么调用isPermutated而不担心返回的值


您的代码永远不会返回true。

请添加导致错误的代码您对发生的情况的假设与实际情况不符。在调试器中对代码进行旋转,看看为什么会发生这种情况。更好的方法是,开始编写Junit测试来练习类并增加复杂性。当调用堆栈溢出时会发生堆栈溢出,这是由有限数量的调用而不是无限数量的调用引起的。如果堆栈大小超过递归调用的数量,则会得到
StackOverflowException
。确切的堆栈大小取决于JVM及其设置,因此我们无法告诉您什么递归深度太大。如果您的代码没有执行任何不必要的递归(使用调试器进行检查),但仍然会出现堆栈溢出,则可以减小问题的大小,或者将算法重写为非递归版本。当我编写不执行停止条件的递归方法时,总是会出现堆栈溢出。请添加导致错误的代码。您对发生的情况的假设与实际情况不符。在调试器中对代码进行旋转,看看为什么会发生这种情况。更好的方法是,开始编写Junit测试来练习类并增加复杂性。当调用堆栈溢出时会发生堆栈溢出,这是由有限数量的调用而不是无限数量的调用引起的。如果堆栈大小超过递归调用的数量,则会得到
StackOverflowException
。确切的堆栈大小取决于JVM及其设置,因此我们无法告诉您什么递归深度太大。如果您的代码没有执行任何不必要的递归(使用调试器进行检查),但仍然会出现堆栈溢出,则可以减小问题的大小,或者将算法重写为非递归版本。当我编写不执行停止条件的递归方法时,总是会出现堆栈溢出。保证你的有缺陷。