How do you make a list of binary numbers? The variables do not accept zeros when initializing. It only adds the numbers. I am trying to get the binary numbers into a list while incrementing them such as 00000000,00000001,00000010,00000011, 00000110, 00000111....
public static void main(String[] args) {
int index = 00000000;
for(int i = 0; i < 255; i++)
{
if(i < 10)
{
if(i % 2 == 0) {
System.out.print(index++);
}
else {
System.out.print(index);
}
if(i > 10 && i < 100)
{
if(i % 2 == 0) {
System.out.print(index++);
}
else {
System.out.print(index);
}
}
}
}
}
}
int n = 10;
for (int i = 1; i <= n; i++) {
System.out.println(String.format("%8s", Integer.toBinaryString(i)).replace(' ', '0'));
}
Outputs:
00000001
00000010
00000011
00000100
00000101
00000110
00000111
00001000
00001001
00001010
Read more on .toBinaryString() .
Get it into a list as suggested here :
List<String> binaryNums = IntStream.range(0, 256)
.mapToObj(n -> String.format("%8s", Integer.toBinaryString(n))
.replace(' ', '0')).collect(Collectors.toList());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.