java code
class Date1{
int year;
int month;
int day;
Date1 (int y,int m,int d){
year=y;
month=m;
day=d;
}
public int compare(Date1 d){
return year>d.year? 1
:year<d.year? -1
:month>d.month? 1
:month<d.month? -1
:day>d.day? 1
:day<d.day? -1:0;
}
}
public class TestBs{
public static void main(String[] args){
Date1 [] d=new Date1 [5];
d[0]=new Date1(2007,7,8);
d[1]=new Date1(2008,8,8);
d[2]=new Date1(2005,8,1);
d[3]=new Date1(2004,6,3);
d[4]=new Date1(2008,6,19);
for(int i=d.length-1;i<=1;i--){
for(int j=0;j<=i-1;j++){
if(d[j].compare(d[j+1])>0){
Date1 temp=d[j];
d[j]=d[j+1];
d[j+1]=temp;
}
}
}
System.out.print("从小到大排序:");
for(int i=0;i<d.length;i++){
System.out.print(" "+d[i]);
}
}
}
运行结果:
从小到大排序: Date1@bd0108 Date1@8ed465 Date1@11a698a Date1@107077e Date1@7ced01
[color=#FF0000]怎么出现这个结果???[/color]
class Date1{
int year;
int month;
int day;
Date1 (int y,int m,int d){
year=y;
month=m;
day=d;
}
public int compare(Date1 d){
return year>d.year? 1
:year<d.year? -1
:month>d.month? 1
:month<d.month? -1
:day>d.day? 1
:day<d.day? -1:0;
}
}
public class TestBs{
public static void main(String[] args){
Date1 [] d=new Date1 [5];
d[0]=new Date1(2007,7,8);
d[1]=new Date1(2008,8,8);
d[2]=new Date1(2005,8,1);
d[3]=new Date1(2004,6,3);
d[4]=new Date1(2008,6,19);
for(int i=d.length-1;i<=1;i--){
for(int j=0;j<=i-1;j++){
if(d[j].compare(d[j+1])>0){
Date1 temp=d[j];
d[j]=d[j+1];
d[j+1]=temp;
}
}
}
System.out.print("从小到大排序:");
for(int i=0;i<d.length;i++){
System.out.print(" "+d[i]);
}
}
}
运行结果:
从小到大排序: Date1@bd0108 Date1@8ed465 Date1@11a698a Date1@107077e Date1@7ced01
[color=#FF0000]怎么出现这个结果???[/color]
System.out.print(" "+d[i]); 打印出的的是这个对象的地址,
System.out.print("从小到大排序:");
for (int i = 0; i < d.length; i++) {
System.out.print(" " + d[i].toDate1());
} public String toDate1() {
return year + "年" + month + "月" + day + "日";
}