开发者

JavaFX unknown loop depth

Is there any easy way to make loop of unknown depth size, so we don't开发者_C百科 have to create this loops, but we can use one function.

var letters = [97..122];

//size 1
for (x in letters) print(x as Character);

//size 2
for (x in letters, y in letters) {
        print(x as Character);
        println(y as Character);
    }

//size 20
for (x1 in letters, .., x20 in letters) {
        print(..);
    }

In the output we have: aa ab ac

and so on.

I know that I have to folow recurison in this way.

Thank you.


public static void printAlphabetCombinations( int recurseCounter, String current ) {
    String temp = current;
    if( recurseCounter == 0 ) {
      System.out.println( temp );
      numberOfCombinations ++;
      return;
    }
    else {
      recurseCounter--;
      for( int i = char_start; i < char_end+1; i++ ) {
        temp = current + (char) (i);
        printAlphabetCombinations( recurseCounter, temp );

      }
    }

Usage:

printAlphabetCombinations(3, "");

Thanks all for help :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜