[Solved] Using Brute Force to generate all possible combination of binary numbers as array? [closed]


First of all, you need to provide a minimal reproducible example of your code. You can check here: https://stackoverflow.com/help/minimal-reproducible-example

In regards to your question, using three loops can be a solution:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        var combinations = new ArrayList<int[]>();
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 2; k++) {
                    combinations.add(new int[] {i, j, k});
                }
            }
        }
    }
}

You can print them like this:

for (var c : combinations) 
{
    System.out.printf("{%d,%d,%d}%n", c[0], c[1], c[2]);
}

3

solved Using Brute Force to generate all possible combination of binary numbers as array? [closed]