import java.util.ArrayList;
public class Subset {
/**Given array*/
String[] list;
/**Number of elements in a given array*/
int count;
/**List to store subsets*/
ArrayList<String> arrayList = new ArrayList<>();
/**
*constructor
* @param list
*/
public Subset(String[] list) {
this.list = list;
count = list.length;
arrayList = new ArrayList<>();
//Find a subset
for(int i=0;i<count;i++) {
execute(i, "");
}
}
/**
*Find a subset using a recall call based on arguments
* @param num
* @param str
*/
public void execute(int num, String str) {
if(num < count) {
if(str == "") {
str = list[num];
}
arrayList.add(str);
for(int i=num+1;i<count;i++) {
execute(i, str + ", " + list[i]);
}
}
}
}
public class Main {
public static void main(String args[]) {
String[] list = {"A", "B", "C", "D"};
Subset subset = new Subset(list);
for(String str:subset.arrayList) {
System.out.println(str);
}
}
}
A
A, B
A, B, C
A, B, C, D
A, B, D
A, C
A, C, D
A, D
B
B, C
B, C, D
B, D
C
C, D
D
Recommended Posts