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

时间:2017-03-31 17:41:36

标签: java string loops input

所以我尝试使用AList参数创建AClass(es) 7-switch String,其中每个字符都在[1]或关闭[0],例如:{{1} }。

0011010

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

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

2 个答案:

答案 0 :(得分:3)

您可以使用==10692== Memcheck, a memory error detector ==10692== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==10692== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info ==10692== Command: ./chain_hash_table.out ==10692== ==10692== ==10692== HEAP SUMMARY: ==10692== in use at exit: 72 bytes in 3 blocks ==10692== total heap usage: 10 allocs, 7 frees, 376 bytes allocated ==10692== ==10692== 24 bytes in 1 blocks are definitely lost in loss record 2 of 3 ==10692== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==10692== by 0x4007EE: htable_insert (htable.c:53) ==10692== by 0x400BD2: main (main.c:14) ==10692== ==10692== 48 (24 direct, 24 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 3 ==10692== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==10692== by 0x4007EE: htable_insert (htable.c:53) ==10692== by 0x400C25: main (main.c:18) ==10692== ==10692== LEAK SUMMARY: ==10692== definitely lost: 48 bytes in 2 blocks ==10692== indirectly lost: 24 bytes in 1 blocks ==10692== possibly lost: 0 bytes in 0 blocks ==10692== still reachable: 0 bytes in 0 blocks ==10692== suppressed: 0 bytes in 0 blocks ==10692== ==10692== For counts of detected and suppressed errors, rerun with: -v ==10692== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0) Integer.toBinaryString(int i)完成字符串,左边为0:

String.format

这将告诉你:

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

答案 1 :(得分:0)

以下是使用二进制表示的解决方案:

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        for (int i = 0; i < 128; i++) {
            System.out.println(padLeft(Integer.toBinaryString(i), 8));
        }
    }

    // borrowed from http://stackoverflow.com/questions/388461/how-can-i-pad-a-string-in-java
    public static String padLeft(String s, int n) {
        return String.format("%1$" + n + "s", s).replace(' ', '0');  
    }
}

demo