回 帖 发 新 帖 刷新版面

主题:求助下!谢谢哥哥!!

class BubbleSort{
    public static void main(String[] args){
        Date[] days = new Date[5];
        days[0] = new Date(2000,1,1);
        days[1] = new Date(2000,2,2);
        days[2] = new Date(2001,3,3);
        days[3] = new Date(2001,4,4);
        days[4] = new Date(2002,4,6);
        bubbleSort(days);
        for(int i=0;i<days.length;i++){
            System.out.println(days);
        }
    }
}

class Date{
    int year,month,day;
    Date(int y,int m,int d){
        year=y;month=m;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;     
    }
    

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

回复列表 (共1个回复)

沙发



已经调试出来! 问题关闭!

public class BubbleSort{
    public static void main(String[] args){
        Date[] days = new Date[5];
        days[0] = new Date(2000,1,1);
        days[1] = new Date(2000,2,2);
        days[2] = new Date(2001,3,3);
        days[3] = new Date(2001,4,4);
        days[4] = new Date(2002,4,6);
        bubbleSort(days);
        for(int i=0;i<days.length;i++){
            System.out.println(days[i]);
        }
    }
        public static Date[] bubbleSort(Date[] a){
            int len=a.length;
            for(int i=len-1;i>=1;i--){
                for(int j=0;j<=i-1;j++){
                    if(a[j].compare(a[j+1]) > 0){
                        Date temp=a[j];
                            a[j]=a[j+1];
                            a[j+1]=temp;
                    }
                }
            }
            return a;
        }
}

class Date{
    int year,month,day;
    Date(int y,int m,int d){
        year=y;month=m;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;     
        }
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册