public class TestDateSort{
public static void main(String[] args){
Date[] days = new Date[3];
days[0] = new Date(2010,2,1);
days[1] = new Date(2011,3,8);
days[2] = new Date(1992,5,6);
Date d = new Date(1992,5,6);
set(days);
for(int i = 0;i<days.length;i++){
System.out.println(days[i]);
}
System.out.println(search(days,d));
}
public static Date[] set(Date[] a){
int len = a.length;
for(int i = len-1;i<=1;i++){
for(int j=0;j<len;j++){
if(a[j].compare(a[j+1])>0){
Date temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
return a;
}
public static int search(Date[] days,Date d){
if(days.length == 0) return 1;
int StartPos = 0;
int EndPos = days.length-1;
int m = (StartPos + EndPos)/2;
while(StartPos <=EndPos){
if(d.compare(days(m)) == 0) return m;
if(d.compare(days(m)) > 0){
StartPos = m +1;
}
if(d.compare(days(m)) < 0){
EndPos = m -1;
}
return -1;
}
}
}
class Date{
int year,month,day;
Date(int y, int k, int d){
year = y; month = k;day = d;
}
public int compare(Date date){
return year>date.year? 1
:year < date.year ? -1
:month > date.month? 1
:month < date.month? -1
:day > date.day? 1
:day < date.day? -1 : 0;
}
public String toString(){
return "Year;Month;Day =="+ year + "-" + month + "-" + day;
}
}
编译提示
TestDateSort.java:37:找不到符号
符号:方法 days(int)
位置:类 TestDateSort
if (d.compare(days(m)) == 0) return m;
还请大虾们赐教!小弟感激涕淋!!!!!!
------解决方案--------------------
days()的参数不是int的吧
------解决方案--------------------
另外,在 Search 方法中最后一个return -1;要放在while 循环语句的外面
------解决方案--------------------
while(StartPos <=EndPos){
if(d.compare(days[m]) == 0){
return m;
}else if(d.compare(days[m])==1){
StartPos = m ;
}else if(d.compare(days[m])==-1){
EndPos = m ;
}
m=(StartPos+EndPos)/2;
}
正确的while循环