| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 554 人关注过本帖
标题:[讨论]显示一个数组的元素,大家有什么方法。
取消只看楼主 加入收藏
狂放不羁
Rank: 4
等 级:贵宾
威 望:12
帖 子:925
专家分:0
注 册:2007-1-24
收藏
 问题点数:0 回复次数:2 
[讨论]显示一个数组的元素,大家有什么方法。

我总结了一下显示数组的方法:
迭代方法:
public static void display(int arrays [] ,int first ,int last){

while(first<=last){
System.out.println(arrays[first++];
}
}


下面是递归方法:
public static void display(int arrays [] ,int first ,int last){

System.out.println(arrays[first]);
if(first<last){

display(arrays,first+1,last);
}
}


public static void display(int arrays[] ,int first,int last){

if(first<=last){

display(arrays,first,last);
System.out.println(arrays[last]);

}
}

public static void display(int arrays [] ,int first ,int last){

if(first==last){
System.out.println(arrays[first]);
}else {

int mid = (first+last)/2;
display(arrays,first,mid);
display(arrays,mid+1,last);
}

}

public static void display(int arrays [] ,int first ,int last){

if(first==last){
System.out.println(arrays[first]);
}else {

int mid = (first+last)/2;
display(arrays,first,mid-1);
System.out.println(arrays[mid]);
display(arrays,mid+1,last);
}

}



大家把其他好的方法都说一下。。一起学习学习。。

[此贴子已经被作者于2007-8-1 13:11:46编辑过]

搜索更多相关主题的帖子: 元素 
2007-08-01 13:09
狂放不羁
Rank: 4
等 级:贵宾
威 望:12
帖 子:925
专家分:0
注 册:2007-1-24
收藏
得分:0 
大家还有好的方法补上去。大家一起学习。。
2007-08-01 13:13
狂放不羁
Rank: 4
等 级:贵宾
威 望:12
帖 子:925
专家分:0
注 册:2007-1-24
收藏
得分:0 


又想到一种:

public static void display(int arrays [] ,int first ,int last){

if(first=last){
System.out.println(arrays[first]);
}else if(first<last) {

System.out.println(arrays[first]);

display(arrays,first+1,last-1);

System.out.pirntln(arrays[last]);
}else {

}
}

[此贴子已经被作者于2007-8-1 13:36:42编辑过]

2007-08-01 13:30
快速回复:[讨论]显示一个数组的元素,大家有什么方法。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011327 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved