当我尝试运行 TestRun 时,出现以下错误。我尝试了很多,但我找不到它。帮我!我得到内存不足错误 Java 堆。运行以下代码也需要更多时间来运行,最后它显示如下所示的错误。如何使这段代码正确?调试这对我来说非常困难。线程“主”java.lang.OutOfMemoryError 中的异常:Java 堆空间。
package website;
import java.util.ArrayList;
import Combination.averageM;
import Combination.boatandstreamM;
import Combination.fractionsM;
import Combination.interestM;
import Combination.measurementM;
import Combination.numberM;
import Combination.percentageM;
import Combination.profitandlossM;
import Combination.simplificationM;
import Combination.timeanddistanceM;
import Combination.timeandwM;
public class ArrayOfSums {
ArrayList<sums> als;
String Q, A, scale, finalvalue;
double fv;
NewOptions pencil = new NewOptions();
sums sumsObj;
public ArrayList<sums> giveArrayOfSums(int x) {
als = new ArrayList<sums>();
int n = 0;
switch (x) {
case 1:
simplificationM pen = new simplificationM();
while (n <= 19) {
pen.simplificationM();
pencil.optionsCreator(pen.fv, pen.scale);
sumsObj = new sums(pen.Q, pen.A, pen.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
als.add(sumsObj);
}
break;
case 2:
numberM pen1 = new numberM();
while (n <= 19) {
pen1.numberM();
pencil.optionsCreator(pen1.fv, pen1.scale);
sumsObj = new sums(pen1.Q, pen1.A, pen1.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
als.add(sumsObj);
}
break;
case 3:
percentageM pen2 = new percentageM();
while (n <= 19) {
pen2.percentageM();
pencil.optionsCreator(pen2.fv, pen2.scale);
sumsObj = new sums(pen2.Q, pen2.A, pen2.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
als.add(sumsObj);
}
break;
case 4:
numberM pen3 = new numberM();
while (n <= 19) {
pen3.numberM();
pencil.optionsCreator(pen3.fv, pen3.scale);
sumsObj = new sums(pen3.Q, pen3.A, pen3.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
als.add(sumsObj);
}
break;
case 5:
fractionsM pen4 = new fractionsM();
while (n <= 19) {
pen4.fractionsM();
pencil.optionsCreator(pen4.fv, pen4.scale);
sumsObj = new sums(pen4.Q, pen4.A, pen4.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
als.add(sumsObj);
}
break;
case 6:
profitandlossM pen5 = new profitandlossM();
while (n <= 19) {
pen5.profitandlossM();
pencil.optionsCreator(pen5.fv, pen5.scale);
sumsObj = new sums(pen5.Q, pen5.A, pen5.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
als.add(sumsObj);
}
break;
case 7:
averageM pen6 = new averageM();
while (n <= 19) {
pen6.averageM();
pencil.optionsCreator(pen6.fv, pen6.scale);
sumsObj = new sums(pen6.Q, pen6.A, pen6.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
als.add(sumsObj);
}
break;
case 8:
timeandwM pen7 = new timeandwM();
while (n <= 19) {
pen7.timeandwM();
pencil.optionsCreator(pen7.fv, pen7.scale);
sumsObj = new sums(pen7.Q, pen7.A, pen7.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
als.add(sumsObj);
}
break;
case 9:
timeanddistanceM pen8 = new timeanddistanceM();
while (n <= 19) {
pen8.timeanddistanceM();
pencil.optionsCreator(pen8.fv, pen8.scale);
sumsObj = new sums(pen8.Q, pen8.A, pen8.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
als.add(sumsObj);
}
break;
case 10:
boatandstreamM pen9 = new boatandstreamM();
while (n <= 19) {
pen9.boatandstreamM();
pencil.optionsCreator(pen9.fv, pen9.scale);
sumsObj = new sums(pen9.Q, pen9.A, pen9.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
als.add(sumsObj);
}
break;
case 11:
interestM pen11 = new interestM();
while (n <= 19) {
pen11.interestM();
pencil.optionsCreator(pen11.fv, pen11.scale);
sumsObj = new sums(pen11.Q, pen11.A, pen11.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3,
pencil.jRadioButton4);
als.add(sumsObj);
}
break;
case 12:
measurementM pen12 = new measurementM();
while (n <= 19) {
pen12.measurementM();
pencil.optionsCreator(pen12.fv, pen12.scale);
sumsObj = new sums(pen12.Q, pen12.A, pen12.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3,
pencil.jRadioButton4);
als.add(sumsObj);
}
break;
}
return als;
}
}
班级总和:
package website;
public class sums {
public String Question, Answer, Finalvalue, P1, P2, P3, P4;
public int choosenOption;
public sums(String Q, String A, String finalvalue, String p1, String p2, String p3, String p4) {
Question = Q;
Answer = A;
Finalvalue = finalvalue;
P1 = p1;
P2 = p2;
P3 = p3;
P4 = p4;
}
public void setChoosen(int n) {
choosenOption = n;
}
}
类测试运行:
package website;
import java.util.ArrayList;
public class TestRun {
/**
* @param args
*/
public static void main(final String[] args) {
ArrayOfSums SumsTable = new ArrayOfSums();
//session.setAttribute("sumtin",SumsTable.giveArrayOfSums(8));
ArrayList<sums> SumsSet = (ArrayList<sums>) SumsTable.giveArrayOfSums(8);
sums gotSum = (sums) SumsSet.get(1);
System.out.println(gotSum.Question);
}
}