请大家帮我看看这道题

linsenlinwei 2010-10-29 03:48:05
class Date{
int year;
int mouth;
int day;
Date(int _year,int _mouth, int _day){
year=_year;
mouth=_mouth;
day=_day;
}

public String toString() {
return "Year:Month:Day -- " + year + "-" + mouth + "-" + day;
}
public int compare(Date d){
return this.year>d.year ? 1
: this.year<d.year ? -1
: this.mouth>d.mouth ? 1
: this.mouth<d.mouth ? -1
: this.day>d.day ? 1
: this.day<d.day ? -1 :0;
}
}


public class TestDateSort2{
public static void main(String args[]){
String temp;
Date days[]=new Date[6];
days[0]=new Date(2001, 11, 2);
days[1]=new Date(2009,10, 2);
days[2]=new Date(2008, 2, 2);
days[3]=new Date(2007, 3, 3);
days[4]=new Date(2006, 4, 5);
for(int i=0;i<days.length;i++){
/11/ temp=days[i].toString();
System.out.println(temp);

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

}

public static void selectSort(Date a[]){
Date temp=a[0];
for(int i=0;i<a.length;i++){
for(int j=i+1;j<a.length;j++){
if(a[j-1].compare(a[j])>0){
temp=a[j];


}


}
if(!(a[i].equals(temp))){
a[i]=temp;
}
}
}
}

运行时出现at TestDateSort2.main(TestDateSort2.java:35) 就是/11/那行报错,如果把/11/哪行注释又出现 at Date.compare(TestDateSort2.java:15)
at TestDateSort2.selectSort(TestDateSort2.java:51)
at TestDateSort2.main(TestDateSort2.java:39)
希望有人帮我看看
...全文
211 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
linsenlinwei 2010-10-29
  • 打赏
  • 举报
回复
谢谢大家的回复,原来是数组问题,我居然一直没有注意到那,小程序是排序的,因为数组那个错去一直没有找出来,所以其他算法上的错去也没有改正,现在都好了,谢谢大家.
linsenlinwei 2010-10-29
  • 打赏
  • 举报
回复
class Date{
int year;
int mouth;
int day;
Date(int _year,int _mouth, int _day){
year=_year;
mouth=_mouth;
day=_day;
}

public String toString() {
return "Year:Month:Day -- " + year + "-" + mouth + "-" + day;
}
public int compare(Date d){
return this.year>d.year ? 1
: this.year<d.year ? -1
: this.mouth>d.mouth ? 1
: this.mouth<d.mouth ? -1
: this.day>d.day ? 1
: this.day<d.day ? -1 :0;
}

/* public boolean equals(Date d){
if((this.year==d.year)&&(this.mouth==d.mouth)&&(this.day==d.day)){
return true;
}else {return false;}
}*/
}


public class TestDateSort2{
public static void main(String args[]){
String temp;
Date days[]=new Date[5];
days[0]=new Date(2000, 11, 2);
days[1]=new Date(2009,10, 2);
days[2]=new Date(2008, 2, 2);
days[3]=new Date(2007, 3, 3);
days[4]=new Date(2006, 4, 5);
for(int i=0;i<days.length;i++){
temp=days[i].toString();
System.out.println(temp);

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

}

public static void selectSort(Date a[]){
for(int i=0;i<a.length;i++){
Date temp;
temp=a[i];
int last=0;;


for(int j=i+1;j<a.length;j++){
if(temp.compare(a[j])>0){
temp=a[j];
last=j;
}
}

if((a[i].equals(temp))==false){
a[last]=a[i];
a[i]=temp;
}
}
}

}

zheng192004 2010-10-29
  • 打赏
  • 举报
回复
/11/ temp=days[i].toString();
不知道“/11/”代表什么意思~
Date days[]=new Date[6];
days[0]=new Date(2001, 11, 2);
days[1]=new Date(2009,10, 2);
days[2]=new Date(2008, 2, 2);
days[3]=new Date(2007, 3, 3);
days[4]=new Date(2006, 4, 5);
要给它6个参数可是只给它5个,循环的时候肯定报错啊
dinglimin2009 2010-10-29
  • 打赏
  • 举报
回复
Date(int _year,int _mouth, int _day){
this.year=_year;//楼主此处最好这样改过来
this.mouth=_mouth;
this.day=_day;
}

sunyiz 2010-10-29
  • 打赏
  • 举报
回复
Date days[]=new Date[6];
days[0]=new Date(2001, 11, 2);
days[1]=new Date(2009,10, 2);
days[2]=new Date(2008, 2, 2);
days[3]=new Date(2007, 3, 3);
days[4]=new Date(2006, 4, 5);
定义了一个长度6的数组,但是只初始化了5个,
后面的程序引用到了days[5],所以出错
kebin0001 2010-10-29
  • 打赏
  • 举报
回复
Date days[]=new Date[6];
days[0]=new Date(2001, 11, 2);
days[1]=new Date(2009,10, 2);
days[2]=new Date(2008, 2, 2);
days[3]=new Date(2007, 3, 3);
days[4]=new Date(2006, 4, 5);
你宣告了6個,卻只用了5個,所以最後一個是null。

11行注釋的話,temp就沒初始化,編譯不過。
ScAREcrOw_ss 2010-10-29
  • 打赏
  • 举报
回复
先抢沙发。。然后帮你看看去哈~~~~

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧