java函数返回数组的问题,新手请教

CodeInfo_ 2013-03-07 02:27:41

public class Test3{
public static void main(String args[]){
Date date2[];
date2=new Date[5];
date2[0]=new Date(2013,1,4);
date2[1]=new Date(2013,2,2);
date2[2]=new Date(2013,1,3);
date2[3]=new Date(2013,1,1);
date2[4]=new Date(2013,2,5);
date2=date2.compare2(date2);//错误。。。。
date2.compare2(date2);//也是错误,为什么,那么该如何指向返回的数组呢
date2=&date2.compare2(date2);//错误。。。。
for(int i=0;i<date2.length;i++){
System.out.println(date2[i]);
}
}
}
class Date{
int year,month,day;
Date(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public int compare(Date date){
....省略
}
public Date[] compare2(Date[] a){
Date temp;
for(int i=0;i<a.length;i++)
for(int j=i+1;j<a.length;j++){
if(a[i].compare(a[j])==1){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
return a;
}
public String toString(){
return(year+"-"+month+"-"+day);
}
}
...全文
341 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZZZ5512536 2013-03-07
  • 打赏
  • 举报
回复
引用 4 楼 a8082649 的回复:
引用 2 楼 breakorcontinue 的回复:Java code?12345678910111213141516171819202122232425262728293031323334353637383940414243public class Test3{public static void main(String args[]){Date date2[];da……

// 编辑框不是可以选插入代码吗,选择java,然后把你代码粘贴到里面就行了
CodeInfo_ 2013-03-07
  • 打赏
  • 举报
回复
引用 2 楼 breakorcontinue 的回复:
Java code?12345678910111213141516171819202122232425262728293031323334353637383940414243public class Test3{public static void main(String args[]){Date date2[];date2=new Date[5];date2[0]=ne……
亲,你用的是什么编译器,还是网站的功能?能让每一句代码左边自动编号
jlu_lamp_lamp 2013-03-07
  • 打赏
  • 举报
回复
Date本身就是关键字 这样用会出一些莫名其妙的错误的
长腮的北极熊 2013-03-07
  • 打赏
  • 举报
回复
public class Test3{
public static void main(String args[]){
Date date2[];
date2=new Date[5];
date2[0]=new Date(2013,1,4);
date2[1]=new Date(2013,2,2);
date2[2]=new Date(2013,1,3);
date2[3]=new Date(2013,1,1);
date2[4]=new Date(2013,2,5);
date2=new Date().compare2(date2);//改这里就可以了...
new Date().compare2(date2);//也是错误,为什么,那么该如何指向返回的数组呢
                // date2=&date2.compare2(date2);//错误。。。。
for(int i=0;i<date2.length;i++){
System.out.println(date2[i]);
}
}
}
class Date{
int year,month,day;
Date(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public int compare(Date date){
                  ....省略
}
public Date[] compare2(Date[] a){
Date temp;
for(int i=0;i<a.length;i++)
for(int j=i+1;j<a.length;j++){
if(a[i].compare(a[j])==1){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
return a;
}
public String toString(){
return(year+"-"+month+"-"+day);
}
}
注意Date类不要Package错jar,最好起类名不要用这种关键字...
yyw6637 2013-03-07
  • 打赏
  • 举报
回复
你的compare2这能被Date实例调用,而你用的是Date实例的数组去调用,那能行吗?date2[1].compare2这样可以的

62,621

社区成员

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

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