publicclassMemory{privateTreeSetunused_partion_list=newTreeSet(newUnused_partion_comparator());booleanmalloc(Strings){Iterator
public class Memory {
private TreeSet unused_partion_list=new TreeSet(new Unused_partion_comparator());
boolean malloc (String s){ Iteratorunused_iterator=unused_partion_list.iterator();
while(unused_iterator.hasNext()){
Partion_structure temp= unused_iterator.next();
if( unused_partion_list.contains(temp)){
int xx;
}
}
下面是比较器的定义:
import java.util.Comparator;
public int compare(Object o1, Object o2) {
if(o1 instanceof Partion_structure && o2 instanceof Partion_structure)
{
Partion_structure po1=(Partion_structure)o1;
Partion_structure po2=(Partion_structure)o2;
if(po1.size
return -1;
if(po1.size>po2.size)
return 1;
if(po1.size==po2.size ) {
if(po1.start
return -1;
if(po1.start>po2.start)
return 0;
if(po1.start==po2.start)
return 0;
}
}
return 0;
}
}
我的问题是:为什么我调试的时候unused_partion_list.contains(temp)这句总是false?
我找到错误原因了,本该返回1的我却返回了0
if(po1.size==po2.size ) {
if(po1.start
return -1;
if(po1.start>po2.start)
return 0; // 这里本该返回1的;
if(po1.start==po2.start)
return 0;
}
展开