java - Replacing a string parameter with a List for a recursive method -


i struggeling simple problem last 3 hours. rewrote class , replaced 2 string parameters lists. problem is, when calling rekursive method add 1 character first string parameter. , when length of parameter hits length of 7 prints out. string never gets longer 7. replaced integer list since string consisted of numbers. list though keeps getting longer , longer , have no idea why. hope explained properly. if not, ask me please.

the question super easy answer guys.

here first class, works.

    package uebung4;  public class permall_alt {  static int counter = 0;  private static void permutation(string word, string str) {      int n = str.length();      // system.out.println(str + " str");     // system.out.println(word + " word");      if (n == 0) {          if (          (integer.parseint(word.substring(0, 1))) > (integer.parseint(word.substring(1, 2)))                 && (integer.parseint(word.substring(1, 2))) < (integer.parseint(word.substring(2, 3)))                 && (integer.parseint(word.substring(2, 3))) > (integer.parseint(word.substring(3, 4)))                 && (integer.parseint(word.substring(3, 4))) < (integer.parseint(word.substring(4, 5)))                 && (integer.parseint(word.substring(4, 5))) > (integer.parseint(word.substring(5, 6)))                 && (integer.parseint(word.substring(5, 6))) < (integer.parseint(word.substring(6, 7)))          ) {             // system.out.println(word);             counter++;         }      } else {         (int = 0; < n; i++) {             // system.out.println("word: " +word+"\t str charat:             // "+str.charat(i));             // system.out.println(word + str.charat(i) + " \t combined");              system.out.println("substr(0,i): " + str.substring(0, i) + " substr(i+1) " + str.substring(i + 1));             permutation(word + str.charat(i), str.substring(0, i) + str.substring(i + 1));         }     } }  public static void main(string[] args) {     permutation("", "1234567");     system.out.println("anzahl:  " + counter); } } 

and here class edited:

package uebung4;  import java.util.arraylist; import java.util.list;  public class permall {  static int counter = 0;  private static void permutation(list<integer> wordlist, list<integer> lis) {    //  list<integer> wordlist2 = clonelist(wordlist);      int n = lis.size();      if (n == 0) {         string word = "";         (integer : wordlist) {             word += i;         }           if ((integer.parseint(word.substring(0, 1))) > (integer.parseint(word.substring(1, 2)))                 && (integer.parseint(word.substring(1, 2))) < (integer.parseint(word.substring(2, 3)))                 && (integer.parseint(word.substring(2, 3))) > (integer.parseint(word.substring(3, 4)))                 && (integer.parseint(word.substring(3, 4))) < (integer.parseint(word.substring(4, 5)))                 && (integer.parseint(word.substring(4, 5))) > (integer.parseint(word.substring(5, 6)))                 && (integer.parseint(word.substring(5, 6))) < (integer.parseint(word.substring(6, 7)))          ) {             system.out.println(word);             // converttodu(word);             counter++;         }      } else {          (int = 0; < n; i++) {             list<integer> templis = new arraylist<>();             //string tempstring = "";              (int j = 0; j < i; j++) {                 templis.add(lis.get(j));             }             system.out.print("str.substr(0,i): " + templis+"\t");               (int k = + 1; k < lis.size(); k++) {                 templis.add(lis.get(k));                 system.out.print(""+lis.get(k)+", ");             }             system.out.println(templis);              // system.out.println("word "+wordlist + "\t charat:             // "+lis.get(i));             wordlist.add(lis.get(i));            //  system.out.println(wordlist + " \t kombiniert");              permutation(wordlist, templis);             // permutation(word + lis.get(i),templis);         }      } }  public static void main(string[] args) {      list<integer> list = new arraylist<integer>();      int anzahl = 7;      (int = 1; <= anzahl; i++) {         list.add(i);     }      string para = "";     (integer : list) {         para += i;     }      list<integer> abc = new arraylist<>();      permutation(abc, list);     system.out.println("anzahl:  " + counter);  } } 

here solution: took code doing recursive call in string args version , copied logic list args version:

        (int = 0; < n; i++) {             // create copy of wordlist             list<integer> permwordlist = new arraylist<integer>(wordlist);              // equiv "word + str.charat(i)"             permwordlist.add(strlis.get(i));                // create copy of lis             list<integer> permstrlist = new arraylist<integer>(lis);              // equiv "str.substring(0, i) + str.substring(i + 1)"             permstrlist.remove(i);                permutation(permwordlist, permstrlist);         } 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -