android - stack=java.lang.StackOverflowError: stack size 8MB -
i'm getting stackoverflow error, understand fully, issue im not dealing big data, how can error produced ?
i have activity, framelayout, fragment, 3 options.
in fragment, when click on 1 of options, re-create fragment , put random numbers, max 15
, not big, error happens when user click fast on options cause overflow.
this code generating, ideas "enhancing" ? don't know if code bad practice momery using.
private static list<integer> savednumbers; public static void setupsavednumberslist(){ savednumbers = new arraylist<>(); } static list<integer> range; private static void adddiff(int mmax){ range = new arraylist<>(); for(int = 0 ; < mmax ; i++){ range.add(i); } range.removeall(savednumbers); } private static int returnifduplic(int mmax){ adddiff(mmax); return new random().nextint(range.size()); } public static int returnuniquesavednumber(int mmax){ int random = returnrandom(mmax); if(savednumbers != null && savednumbers.size() > 0) { if(doessavednumberexist(random)){ return returnifduplic(mmax); } else { savednumbers.add(random); return random; } } else if (savednumbers != null && savednumbers.size() == 0){ savednumbers.add(random); return random; } else if( savednumbers == null){ setupsavednumberslist(); return returnuniquesavednumber(mmax); } else { return 1; } } private static boolean doessavednumberexist(int number){ for(int s: savednumbers){ if(number == s) return true; } return false; } private static int returnrandom(int mmax){ return new random().nextint(mmax); }
this line recursing if happen random same number have saved:
if(doessavednumberexist(random)){ return returnuniquesavednumber(mmax); }
if have saved 1-15, , max 15 guarantee stack-overflow never satisfy if statement. bad approach of "random number no good, try again", since add more , more numbers add more , more recursions. better approach may have random number used index possible numbers left choose.
Comments
Post a Comment