Reputation: 131
I want to find the 2^n permutations of any size based on user input. I have no idea how to do this. I know that I have to use recursion. Unlike most examples I've seen, the parameter of the function is only the number input, there is no string that can be used to make all the permutations. All the permutations are to be stored in an arrayList and output to the user. i.e (input 3) output: 000 001 010 011 100 101 110 111 Here's the code I have so far:
public static void printBin(int bits) {
ArrayList<String> binaryArray = new ArrayList<String>();
if(bits == 0) {
System.out.println(binaryArray);
}
else {
for (String string2 : binaryArray) {
String comp = "";
if (comp.length() <= string2.length()) {
comp = string2;
string = comp;
}
string.concat("0");
binaryArray.add(string);
printBin(bits - 1);
string.concat("1");
binaryArray.add(string);
printBin(bits-1);
}
}
}
Thanks in advance.
Upvotes: 1
Views: 2286
Reputation: 37845
Well here is the best you can do I think.
import java.util.ArrayList;
public class BinaryList {
public static void main(String[] args) {
try {
if (args.length != 1 || Integer.parseInt(args[0]) < 1)) {
System.err.println("Invalid integer argument");
return;
}
binaryRecursive(Integer.parseInt(args[0]), new ArrayList<String>(0));
} catch (NumberFormatException e) {
System.err.println("Argument not an integer");
}
}
public static void binaryRecursive(int bits, ArrayList<String> list) {
if (list.size() == (int)Math.pow(2, bits)) {
for (String n : list) {
System.out.println(n);
}
} else {
StringBuilder n = new StringBuilder(bits);
for (int i = bits - 1; i >= 0; i--) {
n.append((list.size() >> i) & 1);
}
list.add(n.toString());
binaryRecursive(bits, list);
}
}
}
There's no way to keep a list of them without either passing the last as a parameter, returning a value or keeping the list as a field.
Following the logic through for bits == 2 what you get is this:
* 1st method call
list.size() == 0
for 1 to 0 {
(00 >> 1 = 00) & 01 == 0
(00 >> 0 = 00) & 01 == 0
}
list.add("00")
* 2nd method call
list.size() == 1
for 1 to 0 {
(01 >> 1 = 00) & 01 == 0
(01 >> 0 = 01) & 01 == 1
}
list.add("01")
* 3rd method call
list.size() == 2
for 1 to 0 {
(10 >> 1 = 01) & 01 == 1
(10 >> 0 = 10) & 01 == 0
}
list.add("10")
* 4th method call
list.size() == 3
for 1 to 0 {
(11 >> 1 = 01) & 01 == 1
(11 >> 0 = 11) & 01 == 1
}
list.add("11")
* 5th method call
list.size() == 4 == 2 ^ 2
print the list
Upvotes: 1
Reputation: 4500
You start out with n.. I want n=3 bits. Your main function calls a function, "genBin", passing one arguments of ("0", n-1) and the other a ("1",n-1). genBin returns a List. The main function merges the two lists as one ArrayList.
genBin(String in, int bitsLeft), if bitsLeft > 0, calls itself with ("0"+in,bitsLeft-1) and ("1"+in,bitsLeft-1) and merges the two return values as an ArrayList. If bitsLeft == 0, it returns in as a single element ArrayList.
Your main function, upon doing that merge I previously mentioned, ends up with a merged List of all the permutations.
Upvotes: 0