我有一个 Jave 类,它计算给定元素数组的所有可能组合,为此它使用递归方法。它工作正常,但是当输入元素的数量增加时,我发现内存不足问题。我想做的是计算给定大小的块的组合。我的问题是我不知道如何保存和恢复递归方法的状态,特别是当它的调用深度很高时。Beolw 是代码。非常感谢。
package uty;
import java.io.FileOutputStream;
import java.util.ArrayList;
public class ESCalcCombination {
int iMax = 0;
boolean bEnd = false;
int iLenInp;
ArrayList<Integer[]> resultList;
public ESCalcCombination(int[] inElements, int inMaxElem, int inMaxElemLen) {
if (inMaxElem > 0) {
iMax = inMaxElem;
} else {
iMax = new Double(Math.pow(2d, new Integer(inElements.length).doubleValue())).intValue();
}
resultList = new ArrayList(iMax);
iLenInp = inElements.length;
for (int i = 1; i <= iLenInp; i++) {
if (inMaxElemLen > 0) {
if (i > inMaxElemLen) {
break;
}
}
for (int j = 0; j < iLenInp; j++) {
if ((iLenInp - j) < i) {
break;
}
addNextElement(inElements, j, i, null);
if (bEnd) {
break;
}
}
if (bEnd) {
break;
}
}
}
private void addNextElement(int[] inElements, int inCurIndex, int inLimitLen, ArrayList<Integer> inCurrentCombination) {
if (inCurrentCombination != null
&& (inCurrentCombination.size() + (iLenInp - inCurIndex)) < inLimitLen) {
return;
}
ArrayList<Integer> alCombinationLoc = new ArrayList();
if (inCurrentCombination != null) {
alCombinationLoc.addAll(inCurrentCombination);
}
alCombinationLoc.add(inElements[inCurIndex]);
if (alCombinationLoc.size() == inLimitLen) {
Integer[] arComb = new Integer[alCombinationLoc.size()];
arComb = alCombinationLoc.toArray(arComb);
resultList.add(arComb);
alCombinationLoc.clear();
alCombinationLoc = null;
if (resultList.size() == iMax) {
bEnd = true;
}
return;
}
for (int i = ++inCurIndex; i < iLenInp; i++) {
addNextElement(inElements, i, inLimitLen, alCombinationLoc);
if (bEnd) {
return;
}
}
}
public void close() {
ESUty.closeAL(resultList);
}
public ArrayList<Integer[]> getCombinations() {
return resultList;
}
public static void main(String[] args) {
ESCalcCombination ESCaCo = new ESCalcCombination(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, 0, 15);
FileOutputStream fos = null;
try {
fos = new FileOutputStream("c:\\test\\conbinations.txt");
for (int i = 0; i < ESCaCo.getCombinations().size(); i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < ESCaCo.getCombinations().get(i).length; j++) {
sb.append(ESCaCo.getCombinations().get(i)[j]);
}
System.out.println("elemento " + i + " = " + sb.toString());
fos.write((sb.toString() + System.getProperty("line.separator")).getBytes());
}
} catch (Exception ex) {
System.out.println("errore " + ex);
} finally {
ESUty.closeFileOutputStream(fos);
}
System.exit(0);
}
}