I want to print all subsets of the generated arrays recursively in the main method.
The following lines show my Code. I don't know how to implement the method subsets() recursively.
public class Main {
// Make random array with length n
public static int[] example(int n) {
Random rand = new Random();
int[] example = new int[n + 1];
for (int i = 1; i <= n; i++) {
example[i] = rand.nextInt(100);
}
Arrays.sort(example, 1, n + 1);
return example;
}
// Copy content of a boolean[] array into another boolean[] array
public static boolean[] copy(boolean[] elements, int n) {
boolean[] copyof = new boolean[n + 1];
for (int i = 1; i <= n; i++) {
copyof[i] = elements[i];
}
return copyof;
}
// Counts all subsets from 'set'
public static void subsets(int[] set, boolean[] includes, int k, int n) {
// recursive algo needed here!
}
public static void main(String[] args) {
// index starts with 1, -1 is just a placeholder.
int[] setA = {-1, 1, 2, 3, 4};
boolean[] includesA = new boolean[5];
subsets(setA, includesA, 1, 4);
}
}