【发布时间】:2016-04-13 09:31:24
【问题描述】:
为什么这不起作用? 我不确定除了 Formula 之外您还需要哪些其他信息是由 char 和 int 提供的,它们构成了一个类型 Term。
// returns true if f is identical to this Formula
// e.g. terms = {Term('C',2),Term('H',6)} and f = {Term('C',2),Term('H',6)} would return true
// but terms = {Term('C',2),Term('H',6)} and f = {Term('H',6),Term('C',2)} would return false
public boolean identical(Formula f)
{
int fSize = f.getTerms().size();
if(fSize!=terms.size())
{
return false;
}
else
{
for(int j = 0; j < fSize; j++)
{
Term tester = terms.get(j);
Term fTester = f.getTerms().get(j);
if(fTester == tester)
{
continue;
}
else
{
return false;
}
}
}
return true;
}
注意terms 是 ArrayList 的名称
【问题讨论】:
-
可能是因为您使用 == 运算符比较对象,而不是一个好的 equals 实现
-
该死,然后它说 Term 不能转换为布尔值。
-
可能是因为
if(fTester == tester)比较的是地址而不是内容。您需要实现equals并使用它来比较自定义对象。 -
"那么" ?什么时候? = 不是一个 equals 实现,它是一个用于赋值的运算符。
-
如何使用equals方法?