最快的方法来生成大小为n的所有二进制字符串成布尔数组? [英] Fastest way to generate all binary strings of size n into a boolean array?

查看:121
本文介绍了最快的方法来生成大小为n的所有二进制字符串成布尔数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我想长度为3的所有二进制字符串我可以简单地宣布他们是这样的:

For example, if I wanted all binary strings of length 3 I could simply declare them like this:

boolean[] str1 = {0,0,0};
boolean[] str2 = {0,0,1};
boolean[] str3 = {0,1,0};
boolean[] str4 = {0,1,1};
boolean[] str5 = {1,0,0};
boolean[] str6 = {1,0,1};
boolean[] str7 = {1,1,0};
boolean[] str8 = {1,1,1};

什么是产生长度为N的所有可能的二进制串入的布尔数组的最有效方法

我不一定需要的的最有效的方法,只有一个那是相当有效的,并容易让我多线程。

I don't necessarily need the most efficient method, just one that's fairly efficient and easy for me to multithread.

编辑:我要指出,我将它们存储在所有一个ArrayList,如果该事项

I should note that I will be storing them all in an ArrayList, if that matters.

推荐答案

下面是一些code生成真值表...(适用于因为数组大小的限制仅适用于32位(你可以改变大小可变,如果你想要的任何和存储布尔为1/0):

Here's some code to generate a truth table... (works for only for 32 bits because of array size limits ( you can change the size variable to whatever and store booleans as 1/0 if you want):

int size = 3;
    int numRows = (int)Math.pow(2, size);
    boolean[][] bools = new boolean[numRows][size];
    for(int i = 0;i<bools.length;i++)
    {
        for(int j = 0; j < bools[i].length; j++)
        {
            int val = bools.length * j + i;
            int ret = (1 & (val >>> j));
            bools[i][j] = ret != 0;
            System.out.print(bools[i][j] + "\t");
        }
        System.out.println();
    }

这篇关于最快的方法来生成大小为n的所有二进制字符串成布尔数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆