Java: Combination of recursive loops which has different FOR loop inside; Output: FOR loops indexes

Posted by vvinjj on Stack Overflow See other posts from Stack Overflow or by vvinjj
Published on 2012-06-03T10:14:01Z Indexed on 2012/06/03 10:40 UTC
Read the original article Hit count: 214

Filed under:
|
|
|

currently recursion is fresh & difficult topic for me, however I need to use it in one of my algorithms.

Here is the challenge:

I need a method where I specify number of recursions (number of nested FOR loops) and number of iterations for each FOR loop. The result should show me, something simmilar to counter, however each column of counter is limited to specific number.

ArrayList<Integer> specs= new ArrayList<Integer>();
  specs.add(5); //for(int i=0 to 5; i++)
  specs.add(7);
  specs.add(9);
  specs.add(2);
  specs.add(8);
  specs.add(9); 

public void recursion(ArrayList<Integer> specs){
  //number of nested loops will be equal to: specs.size();
  //each item in specs, specifies the For loop max count e.g:
  //First outside loop will be: for(int i=0; i< specs.get(0); i++)
  //Second loop inside will be: for(int i=0; i< specs.get(1); i++)
  //...
}

The the results will be similar to outputs of this manual, nested loop:

    int[] i;
    i = new int[7];

    for( i[6]=0; i[6]<5; i[6]++){
        for( i[5]=0; i[5]<7; i[5]++){
            for(i[4] =0; i[4]<9; i[4]++){
                for(i[3] =0; i[3]<2; i[3]++){
                    for(i[2] =0; i[2]<8; i[2]++){
                        for(i[1] =0; i[1]<9; i[1]++){
                            //...
                            System.out.println(i[1]+" "+i[2]+" "+i[3]+" "+i[4]+" "+i[5]+" "+i[6]);
                        }
                    }
                }
            }
        }
    }

I already, killed 3 days on this, and still no results, was searching it in internet, however the examples are too different. Therefore, posting the programming question in internet first time in my life. Thank you in advance, you are free to change the code efficiency, I just need the same results.

© Stack Overflow or respective owner

Related posts about java

Related posts about recursion