public String[] getAll(int[] array) {
int n = Array.getLength(array);
int total = (int) Math.pow(2, n);
Logger log = Logger.getLogger("myLog");
log.info(" the total :" + total);
String[] outArray = new String[total];
for (int i = 0; i < total; i++) {
StringBuffer bs = new StringBuffer("");
for (int j = 0; j < n; j++) {
if (((i >> j) % 2) == 1) {
bs.append(String.valueOf(Array.get(array, j)));
}
}
outArray[i] = bs.toString();
}
return outArray;
}
int n = Array.getLength(array);
int total = (int) Math.pow(2, n);
Logger log = Logger.getLogger("myLog");
log.info(" the total :" + total);
String[] outArray = new String[total];
for (int i = 0; i < total; i++) {
StringBuffer bs = new StringBuffer("");
for (int j = 0; j < n; j++) {
if (((i >> j) % 2) == 1) {
bs.append(String.valueOf(Array.get(array, j)));
}
}
outArray[i] = bs.toString();
}
return outArray;
}
以上代码把生成的子集存放到一个字符串数组中,当集合的基数很大时,需要很大内存, 可以根据需要把上面程序改改不要放到数组中,而是边生成边使用
评论