日期:2014-05-20  浏览次数:20564 次

求大侠指正错误 小白求教 谢谢
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);


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

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

  }
  
  }

  return a ;


}


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



}
}

这程序哪儿错了 ,求指教一下

------解决方案--------------------
因为Date 在 testdatesort 类里面,所以你没直接用new Date 创建对象。可以把 Date 设置成 static 内部类,这样就不用创建 testdatesort 就可以直接使用。或者把 Date 方法 testdatesort 的类外部,构造外部类。在或者 用 new testdatesort().new Date(...) 创建Date 实例。
------解决方案--------------------
Date是内部类 实例化之前要先实例化外部类
Java code

                Test test = new Test();
        Date[] days = new Date[5];
        days[0] = test.new Date(2006, 5, 4);

------解决方案--------------------
public static Date[] arraysort (Date[] a){
for (int i=a.length-1; i>=1;i-- ){
for (int j=0; j<=i-1; j++){
if (a[j].sort(a[j+1])>0){
Date temp= a[j];
a[j]=a[j+1];
a[j+1]=temp;

}
  
}
做的时候建个包把Date类放入,用的时候引入自己写的Date类以区分jdk的Date类