java - Why can't I get any output from my recursive method? -


i'm trying insert characters (a, b, c) , permutation of array.

for reason not printing out. i'm sure simple mistake can't find it. appreciative advice.

public static void main(string[] args) {      int [] = new int []{'a','b','c'};     permute(a, 3); }  public static void permute(int[] a, int p){     if(a.length == 0){         return;     }     for(int = 0; < a.length; i++){         char ch = (char) p;         p += a[i];         permute(a,p);         p = ch;     } } 

there several problems approach:

  • you use chars when should use ints , vice versa;
  • the program doesn't contain system.out.print statements, never instruct java program print anything;
  • this isn't program enumerates on possible permutations. in fact generate stack overflow exception (not confused name of site), because length of array never changes, call for part , keep building call stack; and
  • it unclear p means.

an in-line permutation program looks like:

public static void permute(char[] a, int p){     if(p >= a.length) {//we've reached end of array, print         for(int = 0; < a.length; i++) {             system.out.print(a[i]);         }         system.out.println();     } else {         char cp = a[p];         for(int = p; < a.length; i++){             char ci = a[i];             a[p] = ci;             a[i] = cp;             permute(a,p+1);             a[i] = ci;         }         a[p] = cp;     } } 

online jdoodle.

then call method permute(a,0) a list of characters wish permutate.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -