简体   繁体   English

当字长超过4时,为什么此代码会给出stackoverflow错误?

[英]why does this code gives stackoverflow error when word length is beyond 4?

i have written a code to generate all possible combinations of letters of a word without any repetition of any letter or any particular word. 我已经编写了一个代码,以生成单词的所有可能组合,而无需重复任何字母或任何特定单词。 the code is as follows 代码如下

static boolean redcheck(int array[])// checks if letters are repeated
{
    boolean check=true;
    for(int i=0;i<array.length-1;i++)
    {
        for(int j=i+1;j<array.length;j++)
        {
            if(array[i]==array[j])
            {
                check=false;
                break;
            }
        }
    }
    return check;
}

static void repeat(char arr2[],int arr1[],int p)// creates and prints the word
{
    if(redcheck(arr1))
    {
        for(int i=0;i<p;i++)
            System.out.print(arr2[arr1[i]]);
        for(int i=0;i<p;i++)
            System.out.print(arr1[i]);
        System.out.println();
    }
    arr1[p-1]+=1;
    for(int ini=p-1;ini>0;ini--)
    {  
        if(arr1[ini]>p-1)
        {
            arr1[ini-1]+=1;
            arr1[ini]=0;  
        }
    }
    if(arr1[0]>p-1)
        return;

    repeat(arr2,arr1,p);
}

public static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.println("enter word");
    String a=sc.nextLine();
    int num=a.length();
    char arr[]=new char[num];
    for(int c=0;c<a.length();c++)
        arr[c]=a.charAt(c);

    int arr1[]=new int[num];
    for(int i:arr1)
        arr1[i]=0;
    repeat(arr,arr1,num);

}

the code works fine till any word of upto length 4, but when its above four, it throws up a stack overflow error. 该代码可以正常工作,直到长度不超过4的任何单词,但是当其超过4时,它将引发堆栈溢出错误。 after some inspection, the main part of code which is creating the problem is the printing part itself, which is 经过一番检查,产生问题的代码的主要部分是打印部分本身,即

for(int i=0;i<p;i++)
            System.out.print(arr2[arr1[i]]);

i really cant find where i am going wrong. 我真的找不到我要去哪里。 the print statement below the one mentioned above, prints the indexes of the words in order which they will be printed and does not give any error. 上面提到的语句下面的print语句按单词的索引顺序打印它们,并且不会出现任何错误。 i am using the bluej editor, and it seems i have 512MB in stack memory. 我正在使用bluej编辑器,看来我的堆栈内存为512MB。 please help.thanks in advance. 请提前帮助。

EDIT: the error code is java.lang.StackOverflowError: null 编辑:错误代码是java.lang.StackOverflowError:空

With 4 letters (assuming none of them are the same) there are 4^4 = 256 possible combinations of these letters. 对于4个字母(假设它们都不相同),这些字母有4 ^ 4 = 256个可能的组合。 As your code is currently set up, you will recurse at least 256 times before returning a value which will have a big memory cost on your stack. 由于您的代码当前已设置,因此您将至少递归256次,然后返回一个值,该值将在堆栈上占用大量内存。 If you try to scale up to 5 letters (once again assuming none are the same) you will have 5^5 = 3125 possible combinations, etc... The stack overflow error you get is due to the amount of time you recurse. 如果您尝试扩展到最多5个字母(再次假设没有相同的字母),您将有5 ^ 5 = 3125个可能的组合,以此类推。。。您得到的堆栈溢出错误是由于递归的时间所致。

My recommendation: separate your repeat method into two parts: 我的建议:将重复方法分为两部分:

static void printWord(char arr2[],int arr1[],int p) {
    if(redcheck(arr1))
    {
        for(int i=0;i<p;i++)
            System.out.print(arr2[arr1[i]]);
        for(int i=0;i<p;i++)
            System.out.print(arr1[i]);
        System.out.println();
    }
}

and then your repeat method: 然后您的重复方法:

static void repeat(char arr2[],int arr1[],int p)// creates and prints the word
{
    while(arr1[0] < p-1){
        printWord(char arr2[],int arr1[],int p);
        arr1[p-1]+=1; // your looping logic
        for(int ini=p-1;ini>0;ini--)
        {  
            if(arr1[ini]>p-1)
            {
                arr1[ini-1]+=1;
                arr1[ini]=0;  
            }
        }
    }
}

making it non recursive will help you avoid the stack overflow error. 将其设为非递归将有助于您避免堆栈溢出错误。

Additional recommendations: Verify if the input word does not have two of the same letters before running any logic, your code will not find any combinations if I input the word "see" as there are no combinations such that a three letter word can be created with the letters {'s','e','e'} without any of them repeating. 附加建议:在运行任何逻辑之前,请验证输入的单词是否没有两个相同的字母,如果我输入单词“ see”,则代码将找不到任何组合,因为没有组合可以创建三个字母的单词带有字母{'s','e','e'}的字母,而无需重复。 Your redcheck method uses one too many variables: 您的redcheck方法使用了太多变量:

static boolean redcheck(int array[])// checks if letters are repeated
{
    for(int i=0;i<array.length-1;i++)
    {
        for(int j=i+1;j<array.length;j++)
        {
            if(array[i]==array[j])
            {
                return false;
            }
        }
    }
    return true;
}

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

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