我试图创建一个静态数组来保存两个链接列表,这些列表将包含两个不同多项式的项。 Term类有两个表示Coefficient和Exponent的int值。我的问题是每次新的"多项式"它创建了一个新的Array实例。我试图使数组静态,但它给了我一个编译器错误,你不能通过修饰符。我的代码如下。谢谢你的帮助:
public class LinkedListInArrayPolynomial implements PolynomialInterface
{
public boolean isFirst = true;
public Term firstTerm;
public int currentFree = 0;
Term[] PolynomialArray = new Term[100];
static class Term {
public int coef;
public int exp;
public Term next;
public Term(int coefIntput, int expInput) {
this.coef = coefIntput;
this.exp = expInput;
}
public String toString() {
String output = "Coef is " + this.coef + " Exp is " + this.exp;
return output;
}
}
public void addToArray(Term term) {
int marker = 0;
boolean written = false;
while (written = false) {
if (PolynomialArray[marker] == null) {
PolynomialArray[marker] = term;
term.next = firstTerm;
this.firstTerm = term;
written = true;
} else {
marker++;
}
}
}
public LinkedListInArrayPolynomial() {
firstTerm = null;
}
public LinkedListInArrayPolynomial(String input) {
boolean FirstElementPos = true;
int exponent;
int coef;
String tempExp = null;
if (input.charAt(0) == '-') {
input = input.substring(1);
FirstElementPos = false;
}
String inputPolynomial = input.replaceAll("-", "+-");
String[] splitTerms = inputPolynomial.split("\\+");
for (int i = 0; i < splitTerms.length; i++) {
String tempTerm = splitTerms[i];
boolean tempPos;
tempPos = true;
if (tempTerm.contains("-")) {
tempTerm = tempTerm.substring(1);
tempPos = false;
}
int IndexOfexponent = tempTerm.indexOf('^');
if (IndexOfexponent == -1) {
if (tempTerm.indexOf('x') == -1) {
exponent = 0;
} else {
exponent = 1;
}
} else {
tempExp = tempTerm.substring(IndexOfexponent + 1);
exponent = Integer.parseInt(tempExp);
}
String tempTermNoCarrot = tempTerm.replaceAll("\\^" + tempExp, "");
String tempCoef = tempTermNoCarrot.replaceAll("x", "");
coef = Integer.parseInt(tempCoef);
if (tempPos == false || FirstElementPos == false) {
coef = (coef * -1);
tempPos = true;
FirstElementPos = true;
}
Term newTerm = new Term(coef, exponent);
System.out.println("Before insert");
System.out.println(newTerm);
PolynomialArray[currentFree] = newTerm;
System.out.println("After insert");
System.out.println(PolynomialArray[currentFree]
+ " At PolynomialArray[" + currentFree + "]");
currentFree++;
// this.addToArray(newTerm);
}
}
public String toString() {
String tempresult = "";
Term current = firstTerm;
while (current != null) {
if (current.exp == 0) {
tempresult += "+";
tempresult += current.coef;
} else {
tempresult += "+";
tempresult += current.coef;
tempresult += "x^";
tempresult += current.exp;
}
current = current.next;
}
tempresult = tempresult.replaceAll("\\^0", "");
tempresult = tempresult.replaceAll("\\+-", "-");
// System.out.println(" at 0 " + PolynomialArray[0].coef);
System.out.println("tempresult before processing: " + tempresult);
PolynomialInterface processingResult = new ArrayWithExponentAsIndexPolynomial(
tempresult);
return processingResult.toString();
}
@Override
public PolynomialInterface add(PolynomialInterface other) {
// TODO Auto-generated method stub
return null;
}
@Override
public PolynomialInterface subtract(PolynomialInterface other) {
// TODO Auto-generated method stub
return null;
}
@Override
public void readPolynomial() {
// TODO Auto-generated method stub
}
}
**请注意我有多个System.out.print行可供调试。