我有一个名为 Polynomial 的类,其 ArrayList 由术语对象组成,我的测试类中有一个由 Scanner 对象读取的外部文件。扫描器读取 4 个不同关键词的行并采取相应的行动。前任。INSERT 3 2. 调用我的插入方法并打印出 3x^2。现在我有一个带有两个参数的删除方法。当我在测试类中调用该方法时,什么也没有发生,同样的事情被打印出来并且没有被删除。我是错过了什么还是一起做错了?任何帮助是极大的赞赏。
public void delete (int coeff, int expo)
{
for (int i = 0; i<terms.size(); i++)
{
Term current = terms.get(i);
terms.remove(current.getCoeff());
terms.remove(current.getExpo());
}
}
我还有一个 Term 类,它创建一个 term 对象,并有两种方法来获取系数和指数。
这是我的测试类的片段:
public static void main(String[] args) throws IOException
{
// TODO code application logic here
Polynomial polyList = new Polynomial();
Scanner inFile = new Scanner(new File("operations2.txt"));
while(inFile.hasNext())
{
Scanner inLine = new Scanner(inFile.nextLine());
String insert = inLine.next();
if(insert.equals("INSERT"))
{
int coeff = inLine.nextInt();
int expo = inLine.nextInt();
polyList.insert(coeff, expo);
}
if(insert.equals("DELETE"))
{
int coeff = inLine.nextInt();
int expo = inLine.nextInt();
polyList.delete(coeff, expo);
}
}
System.out.println(polyList.toString());
}
}
编辑:这是扫描仪类正在读取的 .txt 文件的示例:
INSERT 3 2
INSERT 4 4
INSERT 1 6
INSERT 2 0
INSERT 5 2
INSERT 6 3
PRODUCT
DELETE 3 2
INSERT 2 7
DELETE 4 4
INSERT 4 10
编辑:这是术语类:
class Term
{
//instance vars
private int coefficient;
private int exponent;
public Term(int coeff, int expo)
{
coefficient = coeff;
exponent = expo;
}
public int getCoeff()
{
return coefficient;
}
public int getExpo()
{
return exponent;
}
@Override
public int hashCode()
{
return coefficient + exponent;
}
@Override
public boolean equals(Object o)
{
if (!(o instanceof Term))
{
return false;
}
Term t = (Term)o;
return coefficient == t.coefficient && exponent == t.exponent;
}
}