因此,我尝试使用 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/