具有头部和尾部的可变数量的嵌套循环

时间:2017-01-02 16:15:12

标签: java recursion

我有这些嵌套for循环:

void fun() {

    int n = 5;
    int c = 0;

    for (int i = 0; i < n; i++) {
        head_function(i, c);
        for (int j = 0; j < n; j++) {
            head_function(j, c);
            for (int w = 0; w < n; w++) {
                head_function(w, c);
                for (int x = 0; x < n; x++) {
                    head_function(x, c);

                    c++;
                    body(c);

                    tail_function(x, c);
                }
                tail_function(w, c);
            }
            tail_function(j, c);
        }
        tail_function(i, c);
    }
}

头部和尾部功能的作用并不重要,只要它们可以跟踪它们的指数i,j,w,x。

我想要的是拥有任意数量的嵌套for循环,而不仅仅是四个。

我在这里找到的其他解决方案并不适用于我,因为我认为它们不包括头部和尾部功能。

3 个答案:

答案 0 :(得分:1)

这是一个让你入门的骨架。您可以根据需要随意添加参数并更改签名。

public void doStuffRecursively(int nTimes){
    doIt(nTimes);
}

void doIt(int depth){
    if(depth==0)
        body();
    else{
        head(depth);
        doIt(depth-1);
        tail(depth);
    }
}

void body(){}

void head(int depth){}
void tail(int depth){}

答案 1 :(得分:1)

这是一个非递归版本,它迭代单个循环索引数组。

你的代码是一个小问题:最里面的循环与其他循环不同,只是它调用body()并递增通用计数器。我通过在我的循环中检查“内部循环”来解释这个问题,这个变量由loop变量为0表示。

我还将您的n从5更改为3(max),只是为了减小输出的大小。我认为其余的代码非常简单。

public class Test {

    public static void main(String[] args) {
        System.out.println("\n Loop 1\n");
        loop( 1 );
        System.out.println("\n Loop 2\n");
        loop( 2 );
        System.out.println("\n Loop 3\n");
        loop( 3 );
    }

    public static void loop( int depth ) {
        int[] indices = new int[depth];
        final int max = 3;
        int count = 0;
        for( int x = 0; x < depth - 1; x++ )
            head( x, count );
        outer:
        for(;;) {
            for( int loop = 0; loop < indices.length; loop++ ) {
                if( indices[loop] < max ) {
                    head( indices[loop], count );
                    if( loop == 0 ) {
                        count++;
                        body( indices[loop], count );
                    }
                    tail( indices[loop], count );
                    indices[loop]++;
                    break;
                } else {
                    if( loop == indices.length - 1 ) break outer;
                    indices[loop] = 0;
                }
            }
        }
    }

    private static void head( int index, int counter ) {
        System.out.printf( "head index=%d count=%d%n", index, counter );
    }
    private static void body( int index, int counter ) {
        System.out.printf( "body index=%d count=%d%n", index, counter );
    }
    private static void tail( int index, int counter ) {
        System.out.printf( "tail index=%d count=%d%n", index, counter );
    }

}

部分输出(它变得相当长):

 Loop 1

head index=0 count=0
body index=0 count=1
tail index=0 count=1
head index=1 count=1
body index=1 count=2
tail index=1 count=2
head index=2 count=2
body index=2 count=3
tail index=2 count=3

答案 2 :(得分:0)

由于n似乎总是相同,你可以尝试(c现在是一个私有字段,n是嵌套for循环的数量):

private int c;

void loopAtDepth(int depth){
    if( depth == 0){
       c++;
       body(c);
    } else {
        for(int i = 0; i < n; i++){
            head_function(i, c);
            loopAtDepth(depth - 1)
            tail_function(i, c);
        }
    }
}
相关问题