Today in the project to use a recursive, unexpectedly did not find the error of the algorithm, recorded. Be free to think about ...
Here is the error code:
The function is that if the string ends in rts_mon->, the last face is truncated rts_mon->
Processing results should be:1 rts_mon-> 2 rts_mon-> 3 rts_mon-> 4 rts_mon-> 5 rts_mon-> 6 rts_mon-> 7 rts_mon->" + "\ r \ n"+"rts_mon-(Admin) >
String str="1 rts_mon-> 2 rts_mon-> 3 rts_mon-> 4 rts_mon-> 5 Rts_mon-> 6 rts_mon-> 7 rts_mon->" + "\ r \ n "+"rts_mon-(Admin) > rts_mon-> rts_mon->rts_mon-> ";
Publicstring jiequ (String data) {if(Data.trim (). EndsWith ("rts_mon->") ) {Data= Data.substring (0, Data.trim (). Length ()-9). Trim (); LOG.I (TAG,"onCreate: Current data:"+temp); jiequ (temp); }Else{log.i (TAG,"onCreate: Current data else:"+data); returndata; } return null; }
The code error is: log.i (TAG, "onCreate: Current data else:" + data ); The data printed here is correct, but runs to return data, and after that the training runs down to return null, and executes multiple return null;
After the modified code is as follows, it runs normally.
Public string jiequ (String data) { if (Data.trim (). EndsWith ("rts_mon->")) { = Data.substring (0, Data.trim (). Length ()-9). Trim (); " OnCreate: Current Data: "+ data" ; return jiequ (data); } Else { "onCreate: Current Data else:" + data ); return data; } }
See Error codes <!---->
Publicstring jiequ (String data) {log.i (TAG,"Function: *******"); if(Data.trim (). EndsWith ("rts_mon->") ) {Data= data.substring (0, Data.trim (). Length ()-9). Trim (); LOG.I (TAG,"OnCreate: Current Data:" +data); jiequ (data); }Else{log.i (TAG,"OnCreate: Current Data else:" +data); returndata; } log.i (TAG,"Return: *******"); return NULL; }
When the last face of the string is more than 6 rts_mon-> , you need to enter the jiequ() Method 7 times, log.i (TAG, "function: *******"); Print out 7 times. log.i (TAG, "return: *******"); Will walk 6 times,
LOG.I (TAG, "onCreate: Current Data else:" + data), go once.
After entering if () {}, walk through the code, will go to return null, only the last walk into else{} return data;
Algorithm problem--recursive algorithm