gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:55:33 24 4
gpt4 key购买 nike

所以我正在尝试创建一个 AListAClass(es) 和一个 7-switch String 参数,其中每个字符都在[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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com