我正在尝试生成一个数组,其中包含整数数组的所有可能组合的数组;不包括空集和原始集本身。这是我写的代码。外部循环运行,直到要包括在子集中的元素数量等于原始集合中的元素数量。外部for循环运行以添加新子集的所有元素,外加一个额外的元素。例如,循环应该经历: A,B,然后添加C,然后添加D;生成A,B,C和A,B,D。然后,子集应该转移到B,C,并添加D,得到B,C,D。这个过程继续下去,直到创建了所有子集。然而,我得到了一个数组索引越界异常。
double pow = Math.pow(2.0, a.length);
char[][] b = new char[(int)pow-2][a.length-1];
int start = 0;
int end = 0;
int includedElements = 1;
int curI = 0;
while(includedElements<=a.length) {
if(end == a.length) {
start = 0;
includedElements+=1;
end = includedElements-1;
}
for(int h = end+1; h<a.length; h++) {
b[curI] = new char[includedElements];
for(int i = start, index = 0; i<=end; i++, index++) {
b[curI][index] = a[i];
}
b[curI][b[curI].length] = a[h];
}
curI++;
start+=1;
end+=1;
}https://stackoverflow.com/questions/35397017
复制相似问题