您的位置:首页 > 其它

第05章 数组 09 练习7

2012-08-19 22:53 204 查看
鱼欲遇雨:每日都学习一点,持之以恒,天道酬勤!不能用电脑时,提前补上!(2012.9.6)

数组作为返回值内存分析



public class TestDateSort {
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(2005, 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]);
}
}

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;
}
}

/*
public class Test {
public static void main(String args[]) {
Date[] days = new Date[10];
Date day = new Date(2004, 4, 6);
}
}
*/

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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: