Dynamically change the number of nested loops

I don't know if this is a stupid question, but I need to dynamically change the number of for-loops without using recursion.

For example, if n = 3, I need 3 nested for loops.

for(int i=0; i<size; i++){ for(int j=0; j<size-1; j++){ for(int k=0; k<size-2; k++){ //do something } } } 

If n = 5:

 for(int i=0; i<size; i++){ for(int j=0; j<size-1; j++){ for(int k=0; k<size-2; k++){ for(int l=0; l<size-3; l++){ for(int m=0; m<size-4; m++){ //do something } } } } } 

Is there a way to achieve this without recursion? Another question: what is the use of Multiple Dispatch in Java? I am trying to encode something in ONE METHOD, and it should fire different events in different cases of the parameter. NO IF REPORTS / TERNARY OPERATORS / CASES.

NOTE. I can ONLY have one method (part of the problem) and cannot use recursion. Unfortunately.

+7
java dynamic
source share
5 answers

Think about how many times you go through this cycle. It looks like (size!) / (size - n)! :

 int numLoops = 1; for (int i = 0; i < n; i++) { numLoops*= (size - i); } for (int i = 0; i < numLoops; i++) { //do something } 
+3
source share

It depends on what exactly you are trying to do. Recursion can always be replaced with iteration (see this post for examples, using Stack to store state).

But perhaps the modulo (%) operator could work here? those. have one loop that increments the value of variable ( i ), and then other variables are computed using modulo ( i % 3 , etc.). You can use Map to store variable values ​​indirectly if there is a variable number of variables.

+1
source share

You need to create an array of loop counts and increment it manually.

Quick and dirty example:

 public static void nestedFors(int n, int size) { assert n > size; assert size > 0; int[] i = new int[n]; int l = n - 1; while(l >= 0) { if(l == n - 1) { System.out.println(Arrays.toString(i)); } i[l]++; if(i[l] == size - l) { i[l] = 0; l--; } else if(l < n - 1) { l++; } } } 

Replace System.out.println(Arrays.toString(i)) your own code.

You can check it out here: http://ideone.com/IKbDUV

+1
source share

I think you need a backtracking algorithm. But then you replace your nested loops with recursion.

I do not want to post links here, as moderators do not like it.

Look at the “eight queen puzzle” (you can do it on Google), you get my idea.

I know this idea works, since I asked this very question (which you have) for me in many cases, and I have applied it several times successfully.

Here is a small example (I modified it since the previous one was a bit complicated).

 public class Test001 { public static void main(String[] args) { loop(0, 5, 10); } /** * max_level - the max count of nesting loops * size - the size of the collection * * 0 - top most level * level 1 - nested into 0 * level 2 - nested into 1 * ... * and so on. */ private static void loop(int level, int max_level, int size){ if (level > max_level) return; for (int i=0; i<size-level; i++){ System.out.println("Now at level: " + level + " counter = " + i); loop(level + 1, max_level, size); } } } 

But it still uses recursion.


0
source share

This is a bit confusing, but: here is a way to do it without recursion, in one function and without if s.

 public static void no_ifs_no_recursion(int n){ int[] helper = new int[n-1]; int[] pointers = new int[n]; //helper for printing the results int totalsize = 1; for (int loops = 2; loops <= n; loops++){ helper[n - loops] = totalsize; totalsize*=loops; } for (int i=0; i<totalsize; i++){ int carry = i; for (int loops = 0; loops < n-1; loops++){ pointers[loops] = carry/helper[loops]; carry = carry - (pointers[loops]*helper[loops]); } System.out.println(Arrays.toString(pointers)); //or do something else with `i` -> `pointers[0]`, `j` -> `pointers[1]`, `k` -> `pointers[2]` etc.. } } 
0
source share

All Articles