日期:2014-05-20  浏览次数:20693 次

JAVA基础程序问题
一个JAVA简单问题,我是个初学者,下面这段代码编译后说无法比较,不明白是什么意思,哪位高手能帮忙指点以下

public   class   BubbleSortBinary
{
public   static   void   main(String   args[])
{
Date   days[]   =   new   Date[5];
days[0]   =   new   Date(2006,   5,   4);
days[1]   =   new   Date(2006,   7,   4);
days[2]   =   new   Date(2008,   5,   4);
days[3]   =   new   Date(2004,   5,   9);
days[4]   =   new   Date(2004,   5,   4);
bubbleSort(days);

for(int   i=0;   i <days.length;   i++)
{
System.out.println(days[i]);
}

binarySearch(days,   "2006,   7,   4 ");
}

public   static   void   bubbleSort(Date   days[])
{
for(int   i=days.length-1;   i> =1;   i--)
{
for(int   j=0;   j <=i-1;   j++)
{
if(days[j].compary(days[j+1])> 0)
{
Date   temp   =   days[j];
days[j]   =   days[j+1];
days[j+1]   =   temp;
}
}
}
}

public   static   int   binarySearch(Date   days[],   String   s)
{
int   beginposition   =   0;
int   endposition   =   days.length-1;
int   middle   =   (beginposition   +   endposition)/2;

while(beginposition   <   endposition)
{
if(days.length   ==   0)
{
return   -1;
}

if(s   ==   days[middle])//字符串比较
{
return   middle;
}

if(s   > =   days[middle])//字符串比较
{
beginposition   =   middle   +   1;
}

if(s   <=   days[middle])//字符串比较
{
endposition   =   middle   -   1;
}

middle   =   (beginposition   +   endposition)/2;
}

return   -1;

}
}

class   Date
{
int   year,   month,   day;
Date(int   y,   int   m,   int   d)
{
year   =   y;
month   =   m;
day   =   d;
}

public   int   compary(Date   date)
{
if((this.year   >   date.year)||((this.year   ==   date.year)&&(this.month   >   date.month))||((this.year   ==   date.year)&&(this.month   ==   date.month)&&(this.day   >   date.day)))
{
return   1;
}
if((this.year   ==   date.year)&&(this.month   ==   date.month)&&(this.day   ==   date.day))
{
return   0;
}
else
{
return   -1;
}
}

public   String   toString()
{
return   "year:month:day: "+year+ "   "+month+ "   "+day;
}
}

------解决方案--------------------
if(s == days[middle])//字符串比较
{
return middle;
}

if(s > = days[middle])//字符串比较
{
beginposition = middle + 1;