Java - 使用输入掩码 xxxxxxx 循环遍历所有输入,其中 x = 0 或 1

标签 java string loops input

因此,我尝试使用 7-switch String 参数创建 AClass(es)AList ,其中每个字符要么是 on [1] 或关闭 [0],例如:0011010

ArrayList<AClass> AList = new ArrayList<AClass>();    

public BClass() {
    // I believe there is 128 unique ways to arrange between 0000000 and 1111111
    for (int i = 0; i < ?; i++) {
        // I assume I would need to create the String some how here and use that.
        String str;
        AList.add(new AClass("0000000"));

        / * Each loop would create a new one, you get the idea.
        AList.add(new AClass("1000000"));
        AList.add(new AClass("1100000"));
        AList.add(new AClass("1110000"));
        ...
        ...
        AList.add(new AClass("1001010"));
        ...
        ...
        AList.add(new AClass("1111111"));
        */
    }
}

创建所有 128 个唯一参数 AClass 的最有效方法是什么?

已编辑:错误地从 0000001 而不是 1000000 开始

最佳答案

您可以使用 Integer.toBinaryString(int i)String.format 来完成字符串,左侧为 0:

for (int i = 0; i < 128; i++) {
    System.out.println(String.format("%07d", Integer.parseInt(Integer.toBinaryString(i))));
}

这将向您展示:

0000000
0000001
...
...
1111110
1111111

关于Java - 使用输入掩码 xxxxxxx 循环遍历所有输入,其中 x = 0 或 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43146684/

相关文章:

java - 使用 Java 将 String[] 转换为 Arraylist<String>

loops - 如何让 while 语句在 python 3 中再运行一次

Java 将特定类的列表添加到 java.lang.Object 的列表与 java 8 流一起工作 - 为什么?

java - 线性布局网格在模拟器中显示但在设备上不显示,可能是什么原因?

java - Android ListView 搜索法语单词

php - parse_str()突然不起作用

java - 最短编辑距离?我需要它吗?

matlab - 使用 "find"命令累加值 "without for loops"

javascript - 循环 javascript 不起作用

java - 使用java的xsl多输出