当前位置:编程学习 > JAVA >>

求助数组的问题

public class TestDate
{
public static void main(String[] args)

Date[] days=new Date[5];
                .............
                days.f()//为什么这里不能调用f()方法?days到低是不是一个对象?
                ...............
        }
}
clss Date
     {
          ............
         public static Date[] f()
        {
            ............
        }
     }
真心求教各位老师:为什么这里不能调用f()方法?days到低是不是一个对象? class 数组 --------------------编程问答-------------------- 类型不匹配。
days[index].f(); --------------------编程问答-------------------- 老师,好像还是不行,要不我把原编码法给您看一下,谢谢了
public class TestDate
{
public static void main(String[] args)

Date[] days=new Date[5];

days[0]=new Date(2001,1,1);
days[1]=new Date(2001,1,2);
days[2]=new Date(2002,2,3);
days[3]=new Date(2000,3,4);
days[4]=new Date(2005,6,7);
// Date day=new Date(2001,1,2);
days.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;
int month;
int day;
Date(int a,int b,int c)
{
year=a; month=b;  day=c;
}
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 class TestDate
{
public static void main(String[] args)

Date[] days=new Date[5];

days[0]=new Date(2001,1,1);
days[1]=new Date(2001,1,2);
days[2]=new Date(2002,2,3);
days[3]=new Date(2000,3,4);
days[4]=new Date(2005,6,7);
// Date day=new Date(2001,1,2);
days.bubbleSort(days);[size=13px][/size] 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;
int month;
int day;
Date(int a,int b,int c)
{
year=a; month=b;  day=c;
}
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;
  
  }
 
} --------------------编程问答-------------------- bubbleSort是当前类中的一个静态方法,你直接写bubbleSort(days)就行,它不是days的方法,如果前面一定要写点什么,那也是写TestDate.bubbleSort(days) --------------------编程问答--------------------
引用 4 楼 yhp1234 的回复:
bubbleSort是当前类中的一个静态方法,你直接写bubbleSort(days)就行,它不是days的方法,如果前面一定要写点什么,那也是写TestDate.bubbleSort(days)


哦,原来是这样子,终于明白啦,谢谢您 --------------------编程问答--------------------
引用 4 楼 yhp1234 的回复:
bubbleSort是当前类中的一个静态方法,你直接写bubbleSort(days)就行,它不是days的方法,如果前面一定要写点什么,那也是写TestDate.bubbleSort(days)

老师,那我再问你一下,Date[] days这个数组跟是不是也可以理解成:其实跟class Date 这个类没有直接的关系 --------------------编程问答-------------------- 你的bubblesort不需要返回类型为Date[]。数组传参是传引用。
调用静态方法bubblesort直接写bubblesort(days)。不能写days.bubblesort,这样不对。
public class test {
public static void main(String[] args) {
Date[] days = new Date[5];

days[0] = new Date(2001, 1, 1);
days[1] = new Date(2001, 1, 2);
days[2] = new Date(2002, 2, 3);
days[3] = new Date(2000, 3, 4);
days[4] = new Date(2005, 6, 7);
// Date day=new Date(2001,1,2);
bubbleSort(days);
for (int i = 0; i < days.length; i++) {
System.out.println(days[i]);
}
}

public static void 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;
}
}

}
}

}

class Date {
int year;
int month;
int day;

Date(int a, int b, int c) {
year = a;
month = b;
day = c;
}

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;

}
}
--------------------编程问答--------------------
引用 6 楼 wuminnan 的回复:
引用 4 楼 yhp1234 的回复:
bubbleSort是当前类中的一个静态方法,你直接写bubbleSort(days)就行,它不是days的方法,如果前面一定要写点什么,那也是写TestDate.bubbleSort(days)
老师,那我再问你一下,Date[] days这个数组跟是不是也可以理解成:其实跟class Date 这个类没有直接的关系

不能这么理解,days是Date类型的数组,它的每一个元素(days[1]、days[2]、、、)都是Date类型的 --------------------编程问答-------------------- 那如果是这样子呢
public class TestDate
{
public static void main(String[] args)

Date[] days=new Date[5];

days[0]=new Date(2001,1,1);
days[1]=new Date(2001,1,2);
days[2]=new Date(2002,2,3);
days[3]=new Date(2000,3,4);
days[4]=new Date(2005,6,7);
// Date day=new Date(2001,1,2);
days.bubbleSort(days);//这里为什么不行
for(int i=0; i<days.length; i++) {
System.out.println(days[i]);
}
}



}
class Date
{
int year;
int month;
int day;
Date(int a,int b,int c)
{
year=a; month=b;  day=c;
}
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  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 String toString()
  {
   return "Year:Month:Day -- " + year + "-" + month + "-" + day;
  
  }
 
} --------------------编程问答-------------------- 1.方法是静态的,应该用类名.方法,比如你这里应该用Date.bubblesort(days);
2.就算不是静态的,你这里也不能用days.bubblesort(days)。因为days不是Date的对象,而是Date对象的数组。你可以写days[0].bubblesort(days)、days[1].bubblesort(days)。
引用 9 楼 wuminnan 的回复:
那如果是这样子呢
public class TestDate
{
public static void main(String[] args)

Date[] days=new Date[5];

days[0]=new Date(2001,1,1);
days[1]=new Date(2001,1,2);
days[2]=new Date(2002,2,3);
days[……

--------------------编程问答--------------------
Quote: 引用 8 楼 abc41106 的回复:

引用 6 楼 wuminnan 的回复:引用 4 楼 yhp1234 的回复:
bubbleSort是当前类中的一个静态方法,你直接写bubbleSort(days)就行,它不是days的方法,如果前面一定要写点什么,那也是写TestDate.bubbleSort(days
老师,那如果bubbleSort(days)是在class Date类里面的方法呢?像这样:如果一定要加个对象。该怎么加
 public class TestDate
 {
 public static void main(String[] args)
 { 
 Date[] days=new Date[5];

 days[0]=new Date(2001,1,1);
 days[1]=new Date(2001,1,2);
 days[2]=new Date(2002,2,3);
 days[3]=new Date(2000,3,4);
 days[4]=new Date(2005,6,7);
 // Date day=new Date(2001,1,2);
 days.bubbleSort(days);//这里为什么不行
for(int i=0; i<days.length; i++) {
 System.out.println(days[i]);
 }
 }



 }
 class Date
 {
 int year;
 int month;
 int day;
 Date(int a,int b,int c)
 {
 year=a; month=b;  day=c;
 }
 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  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 String toString()
   {
    return "Year:Month:Day -- " + year + "-" + month + "-" + day;
   
   }
  
 }  --------------------编程问答-------------------- 其实你这个还是不要把方法放到Date类里面了。Date只提供日期的处理就行了。
public class TestDate {
public static void main(String[] args) {
TestDate test = new TestDate();//new一个TestDate的对象

Date[] days = new Date[5];

days[0] = new Date(2001, 1, 1);
days[1] = new Date(2001, 1, 2);
days[2] = new Date(2002, 2, 3);
days[3] = new Date(2000, 3, 4);
days[4] = new Date(2005, 6, 7);
// Date day=new Date(2001,1,2);
test.bubbleSort(days);//调用TestDate的成员函数
for (int i = 0; i < days.length; i++) {
System.out.println(days[i]);
}
}

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

}
}

}

class Date {
int year;
int month;
int day;

Date(int a, int b, int c) {
year = a;
month = b;
day = c;
}

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;

}
}
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,