Home > Net >  Why does this nested loop only iterate 14 times?
Why does this nested loop only iterate 14 times?

Time:01-29

I am a Java beginner. I am writing a method that should iterate through every possible value of a byte array, but the limit for each byte number in the array is 13. So, I am using nested loops. Here is the code I currently have:

public static void iterateThroughMoves() {
byte[] moveSet = {0, 0, 0, 0};

        while(moveSet[0] < 14) {
            while(moveSet[1] < 14) {
                while(moveSet[2] < 14) {
                    while(moveSet[3] < 14) {
                        System.out.println(Arrays.toString(moveSet));
    
                        moveSet[3]   ;
                    }
    
                    moveSet[2]   ;
                }
    
                moveSet[1]   ;
            }
    
            moveSet[0]   ;
        }
    }

For some reason, when I run this method, it prints this one the console:

[0, 0, 0, 0]
[0, 0, 0, 1]
[0, 0, 0, 2]
[0, 0, 0, 3]
[0, 0, 0, 4]
[0, 0, 0, 5]
[0, 0, 0, 6]
[0, 0, 0, 7]
[0, 0, 0, 8]
[0, 0, 0, 9]
[0, 0, 0, 10]
[0, 0, 0, 11]
[0, 0, 0, 12]
[0, 0, 0, 13]

Process finished with exit code 0

Shouldn't it print every possible value of the array? And why is it breaking out of the loop? The first while loop won't break unless the value of the first index of the byte array is greater than 13, and, as we can see in the console output, it never ends up being greater than 13.

I honestly don't know what to try. I've been looking a lot at the code, and I can't figure out what is wrong. As I previously mentioned, I am a beginner, so please don't get mad at me if this is an incredibly simple problem with a stupidly simple solution.

CodePudding user response:

You'll need to reset the inner counters (indices 1,2,and 3) if you want to iterate over every possible combination from 0-13 in each index.

    while(moveSet[0] < 14) {
        moveSet[1] = 0;             // RESET
        while(moveSet[1] < 14) {
            moveSet[2] = 0;         // RESET
            while(moveSet[2] < 14) {
                moveSet[3] = 0;     // RESET
                while(moveSet[3] < 14) {
                    System.out.println(Arrays.toString(moveSet));

                    moveSet[3]   ;
                }

                moveSet[2]   ;
            }

            moveSet[1]   ;
        }

        moveSet[0]   ;
   

Another way to print all 14x14x14x14==38416 combinations is to use a single for-loop and make use of modulo arithmetic.

int iterations = 14*14*14*14;  // 38416
for (i = 0; i < iterations; i  ) {
    int [] moveSet = {0,0,0,0};
    moveSet[3] = i % 14;
    moveSet[2] = (i/14) % 14;
    moveSet[1] = ((i/14)/14) % 14;
    moveSet[0] = (((i/14)/14)/14) % 14;
    System.out.println(Arrays.toString(moveSet));
}

CodePudding user response:

When your innermost loop executed once, it'll never meet the condition again: moveSet[3] remains at 14, no more printing is done.

Did you want to set the value back to 0 after the loop? Likely. Also for the other values.

You might want to inspect the values in a single step debugger.

CodePudding user response:

I think your problem lies inner loop values, they are not resetting to 0, so your function will only run 14 times, try to reset your values to 0 after every inner loop/ this should help you print all the possible values of the array Example

 while(moveSet[0] < 14) {
            while(moveSet[1] < 14) {
                while(moveSet[2] < 14) {
                    while(moveSet[3] < 14) {
                        System.out.println(Arrays.toString(moveSet));

                        moveSet[3]   ;
                    }
                    moveSet[3]=0;
                    moveSet[2]   ;
                }
                moveSet[2]=0;
                moveSet[1]   ;
            }
             moveSet[1]=0;
            moveSet[0]   ;
        }
  • Related