递归后的StackOverFlow错误 [英] StackOverFlow error after recursion

查看:100
本文介绍了递归后的StackOverFlow错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有这个代码



Hey, i have this code

private void putTextOnButtons() {
        for (int i = 0; i < 4; i++) {
            int num = rand.nextInt(10) + 1;
            number[i] = num;
            buttons[i].setText(num + "");
            sortArray(number);
            for (int j = 0; j < 3; j++) {
                if (number[j] == number[j + 1])
                    putTextOnButtons();
            }
            sortArray(number);
        }
    }



但每当我尝试运行它时,它会崩溃并说


But whenever i try to run it, it crashes and says

Process: com.example, PID: 12305<br />
  java.lang.StackOverflowError







> at com.example.gal.tapbynumber.EasyMainActivity.putTextOnButtons(EasyMainActivity.java:110)

其中包含`putTextOnButtons();`

下的if。

我在这里看不到任何问题,有人可以帮我吗?


and

> at com.example.gal.tapbynumber.EasyMainActivity.putTextOnButtons(EasyMainActivity.java:110)
Which takes to `putTextOnButtons();`
under the if.
I don't see any problem here, Any one can help me please?

推荐答案

我已经解决了所有我需要做的就是改变
I have solved it all i had needed to do is change from this
private void putTextOnButtons() {
        for (int i = 0; i < 4; i++) {
            int num = rand.nextInt(10) + 1;
            number[i] = num;
            buttons[i].setText(num + "");
            sortArray(number);
            for (int j = 0; j < 3; j++) {
                if (number[j] == number[j + 1])
                    putTextOnButtons();
            }
            sortArray(number);
        }
    }



到此


to this

private void putTextOnButtons() {
        for (int i = 0; i < 4; i++) {
            int num = rand.nextInt(10) + 1;
            number[i] = num;
            buttons[i].setText(num + "");
            }
            sortArray(number);
            for (int j = 0; j < 3; j++) {
                if (number[j] == number[j + 1])
                    putTextOnButtons();
            }
            sortArray(number);
        
    }


在这种情况下,您应该使用循环而不是递归。在第二个循环中设置一个变量,它将指示存在一些重复项,并添加一个外部循环,使用该变量循环所需的整个代码。



因此,生成唯一的随机数并不是一个很好的方法,好像序列很长并且生成的数字范围很小,它可能会导致大量重试甚至几乎无限循环。



鉴于您只生成了一个小数字,最好列出这些数字并在该列表中选择一个数字,而不是所有想要的数字。



类似的问题被多次询问,所以如果你无法解决,你可以使用谷歌。
In that case you should use a loop instead of recursivity. In the second loop set a variable that will indicate that there are some duplicates and add an outer loop that use that variable to loop the whole code of necessary.

By the way, it is not a good way to generate unique random numbers as if the sequence is long and generated numbers range is small, it can lead to a lot of retries or even almost infinite loop.

Given that you generated only small number, better to make a list of those and pick a number into that list untl you have all desired numbers.

Similar questions have been asked multilple times, so if you cannot figure it out, you can use Google.


这篇关于递归后的StackOverFlow错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆