A list of all binary combinations of numbers in Java
I'm working on a project involving "dynamic programming". I'm shocked by this trivial thing. Please help
Suppose I take 4 as input, I want to display something similar to: 0000 to 1111
However, if I enter 5, I want to display as follows: 00000 to 11111, and so on
Thank you in advance,
Editor: please don't post the code It's not a homework problem. I don't need any code. Just tell me its logic and I'll be happy
Edit2: stackoverflow is happening in wth. I asked if you have written code for me? I hope the man who voted for it What's the point of this forum if I can't help?
Share logic with me We can talk about it. I don't need this code
Editor 3: I publish the code I tried here I hope this "satisfaction" to everyone who thinks I haven't tried
import java.util.ArrayList;
Public class regularinvestigator{
public ArrayList createCombinations(ArrayList listOfFlightNumbers){
ArrayList<String> result = new ArrayList<String>();
for(int i = 1; i < listOfFlightNumbers.size(); i++) {
String binaryEqvivalent = Integer.toBinaryString(i);System.out.println(binaryEqvivalent);
String element = "";
for(int j = 0; j < binaryEqvivalent.length(); j++)
if(binaryEqvivalent.charAt(j) == '1')
element += listOfFlightNumbers + " ";
result.add(element.substring(0,element.length() - 1));
}
return result;
}
private String getContent(ArrayList<String> flight) {
String temp = "";
for(int i = 0; i < flight.size() - 1; i++) temp += flight.get(i) + " ";
temp += flight.get(flight.size() - 1);
return temp;
}
private ArrayList removeElementAtIndex(ArrayList flight,int position){
ArrayList<String> res = new ArrayList<String>();
for(int i = 0; i < flight.size(); i++) {
if(i != position) res.add(flight.get(i));
}
return res;
} }
Editor 4: Thank phoxis, pengone, Jerry coffin and oliholz for their valuable answers:)
Solution
>Get input > from I = 0 to (2 ^ n) – 1 count > for each value of the I-bit mask, each bit I and is displayed
