本世纪最大菜鸟提问!进来看下

w15802111174 2007-10-25 08:41:50
创建若干XX的对象,保存载XX类型数组中,依次输出。怎么写?几种写法?

...全文
83 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
西咸 2007-10-25
  • 打赏
  • 举报
回复
楼上的正确
bxdg42578670 2007-10-25
  • 打赏
  • 举报
回复
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(2008, 5, 4);
days[3] = new Date(2004, 5, 9);
days[4] = new Date(2004, 5, 4);
for(int i=0; i<days.length; i++) {
System.out.println(days[i]);
}
}

}

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

62,623

社区成员

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

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