与布尔比较的ArrayList [英] Comparing ArrayLists with Booleans
本文介绍了与布尔比较的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么不工作的呢?
我不知道从公式是由一个char和一个int这使得类型期限给你需要什么样的其他信息分开。
//如果f是相同的这个公式返回true
//例如条款= {期限('C',2),期限('H',6)}和f = {期限('C',2),期限('H',6)}将返回true
//但术语= {期限(C,2),期限(H,6)}和f = {期限(H,6),期限(C,2)}将返回false公共布尔相同(公式f)
{
INT FSIZE = f.getTerms()的大小()。 如果(FSIZE!= terms.size())
{
返回false;
}
其他
{
对于(INT J = 0; J< FSIZE; J ++)
{
长期测试仪= terms.get(J);
。长期fTester = f.getTerms()获得(J); 如果(fTester ==测试仪)
{
继续;
}
其他
{
返回false;
}
}
} 返回true;
}
N.B。来讲是ArrayList的
的名称
块引用>解决方案您需要不使用
==
,但使用比较两个对象的从而使对象的内容进行比较,等于
方法。由于
期限
是自定义类,你需要重写此方法自己:类期限{
焦炭℃; //你的任期类中的两个值
INT I; @覆盖
公共布尔等于(对象o){
//省略检查
期限等=(期限)O;
//现在比较的内容:
回到我== other.i&放大器;&安培; ç== other.c;
}
}然后你可以用它们进行比较
如果(fTester.equals(测试)){
继续;
}Why doesn't this work? I'm not sure what other information you would require apart from Formula is given by a char and an int which makes a type 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; }
N.B. terms is the name of the ArrayList
解决方案You need to compare two objects not using
==
, but using theequals
method so that the objects' contents are compared.Since
Term
is your custom class, you need to override this method yourself:class Term { char c; //the two values inside your Term class int i; @Override public boolean equals(Object o){ //checks omitted Term other = (Term)o; //now compare the contents: return i==other.i && c==other.c; } }
Then you can compare them using
if(fTester.equals(tester)){ continue; }
这篇关于与布尔比较的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文