compareTo()方法报错
public int compareTo(Student o) { // TODO Auto-generated method stub return this.getId().compareTo(o.getId());//返回0 ,负整数、正整数 } 报错:Cannot invoke compareTo(int) on the primitive type int
public int compareTo(Student o) { // TODO Auto-generated method stub return this.getId().compareTo(o.getId());//返回0 ,负整数、正整数 } 报错:Cannot invoke compareTo(int) on the primitive type int
2017-07-10
最后,我是这么实现的:compareTo()方法比较的字符串类型是否相等,而我一开始把id设置成int型,
所以在里面判断语句,返回-1,0,1同样可以达到效果。代码如下:
@Override public int compareTo(Student o) { // TODO Auto-generated method stub //当前的id和比较参数的id比较 int set; if(this.getId()==o.getId()){ set =0; }else if(this.getId()<o.getId()){ set =-1; }else{ set = 1; } return set;//返回-1,0,1
举报