我有一个对象和比较器的列表,用于在列表中进行排序和序列化。 Collections.binarySearch()返回null,它应该返回一个整数值。以下是代码:Java Collections.binarySearch()返回null
List<AttribRow> rows = new ArrayList<AttribRow>();
AttribRow temp = new AttribRow();
temp.setIndv1((long)22);
rows.add(temp);
temp = new AttribRow();
temp.setIndv1((long)22);
rows.add(temp);
temp = new AttribRow();
temp.setIndv1((long)22);
rows.add(temp);
temp = new AttribRow();
temp.setIndv1((long)23);
rows.add(temp);
temp = new AttribRow();
temp.setIndv1((long)22);
rows.add(temp);
temp = new AttribRow();
temp.setIndv1((long)25);
temp.setId((long)55);
Collections.sort(rows, new CitRowsComparator());
int index = 0;
index = Collections.binarySearch(rows, temp,new CitRowsComparator());
AttribRow是映射到表的实体bean类。它有一个用于比较的字段indv1。
private Long indv1;
public Long getIndv1() {
return indv1;
}
public void setIndv1(Long indv1) {
this.indv1 = indv1;
}
这是Comporator类代码
public class CitRowsComparator implements Comparator<AttribRow> {
public CitRowsComparator() {
super();
}
public int compare(AttribRow one, AttribRow two) {
return one.getIndv1().compareTo(two.getIndv1());
}
}
Collections.binarySearch()alsways返回null。即使当我在Comparator中更改compare()方法返回0时,它仍然返回null。在binarySearch envocation之后,用作键的对象温度也为空。我没有任何预期。我尝试了一个字段的其他类相同的代码,它工作正常。 任何帮助将不胜感激。
哪有'binarySearch'回空当它不返回具有空值的类型?你怎么知道'index'是空的而不是0? – 2011-02-23 07:46:39
此代码在删除'temp.setId((long)55)的机器上工作。 ' – 2011-02-23 07:49:42
-1 - 这个问题是报告公然不可能的事情。 – 2011-02-23 07:59:32