Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 编程/算法/API  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 关于FOR、WHILE循环的讨论!
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-11-11 16:50 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
for (boolean more = true; more; ) {
....
if (....) {
....
more = false;
}
.....
}


jameszhang edited on 2004-11-15 09:53

"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"


从Coding Fan到真正的技术专家
作者 Re:这样写FOR循环的举手!! [Re:jameszhang]
Jove



CJSDN高级会员


发贴: 1228
积分: 194
于 2004-11-11 17:17 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
倒,为什么不用while(flag)呢, 真够BT Big Smile



如何用JTable设置表头多行
作者 Re:这样写FOR循环的举手!! [Re:jameszhang]
JiafanZhou



版主


发贴: 736
积分: 61
于 2004-11-11 18:44 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
For loop is always better than while loop.



初学者与<书>
作者 Re:这样写FOR循环的举手!! [Re:HenryShanley]
烂泥





发贴: 538
积分: 72
于 2004-11-11 20:31 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
HenryShanley wrote:
For loop is always better than while loop.

请指教哦




关于 异常 。。。。 谢谢指教
作者 Re:这样写FOR循环的举手!! [Re:jameszhang]
JiafanZhou



版主


发贴: 736
积分: 61
于 2004-11-11 21:27 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
ArrayList al=new ArrayList();
Iterator it=al.iterator();
while(it.hasNext()){
//.......it.next();
}

for(Iterator it=al.iterator();it.hasNext() ; ){
//.......it.next();
}
//局部变量要尽量使自己变成黑箱black box,从这点上来看我们总认为for要比while好一点。


HenryShanley edited on 2004-11-15 21:47


一个程序员的奋斗历程
作者 Re:这样写FOR循环的举手!! [Re:HenryShanley]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2004-11-11 22:47 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
HenryShanley wrote:
ArrayList al=new ArrayList();
Iterator it=al.iterator();
while(it.hasNext()){
//.......it.next();
}

for(Iterator it=al.iterator();it.hasNext() ; ){
//.......it.next();
}
//局部变量要尽量使自己变成黑箱black box,从这点上来看我们总认为for要比while好一点。

{
Iterator it=al.iterator();
while(it.hasNext()) {
//.......it.next();
}
}

has the same effect. Smile
while is good syntatic sugar for clarity.
Anyway, I like to use for than while.




25了才开始学JAVA晚吗?
作者 Re:这样写FOR循环的举手!! [Re:why]
edgeloner





发贴: 69
积分: 10
于 2004-11-12 13:04 user profilesend a private message to usersend email to edgelonersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我iterator一贯喜欢用for,while在实际中我几乎没有怎么用到,能用for我一般不去用while。for感觉要干净些



初学者与<书>
作者 Re:这样写FOR循环的举手!! [Re:jameszhang]
qingbo777

轻薄



发贴: 106
积分: 0
于 2004-11-13 00:52 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我比较喜欢用FOR,几乎从来不用while.


总想让自己变的帅一点,
却发现离这个目标越来越远.

从Coding Fan到真正的技术专家
作者 Re:这样写FOR循环的举手!! [Re:jameszhang]
raulmadrid



发贴: 0
积分: 0
于 2004-11-13 12:06 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
for( ; ; ) {
}
不就可以了么


raulmadrid edited on 2004-11-13 12:29


一个程序员的奋斗历程
作者 Re:这样写FOR循环的举手!! [Re:raulmadrid]
qingbo777

轻薄



发贴: 106
积分: 0
于 2004-11-13 17:30 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
raulmadrid wrote:
for( ; ; ) {
}
不就可以了么

个人认为,用这种方法要跳出的话必须用break,不如人家的方法有节奏感.



总想让自己变的帅一点,
却发现离这个目标越来越远.

Hibernate的优点
作者 Re:这样写FOR循环的举手!! [Re:qingbo777]
littledeer1974

Garbage Collector

CJSDN高级会员


发贴: 1517
积分: 122
于 2004-11-13 18:47 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
qingbo777 wrote:
我比较喜欢用FOR,几乎从来不用while.

可是有的时候用FOR是不是比较明白

比方说线程里的那个
public void run(){
while(true){
// your thread contents
}
}


是不是这样好点呢,还有Stream里边

while(myStream.available()>certainValue){
myStream.read(yourBuffer);
}


我觉得用while好像更自然易懂一些
:)不知道大家编写程序时,还有什么习惯呢



Surround yourself with people who are open to change


[转帖]面向对象的思维方法
作者 Re:这样写FOR循环的举手!! [Re:jameszhang]
zangweiren





发贴: 1
积分: 0
于 2004-11-13 21:27 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我觉得只要不影响效率,用哪种方法就是大家的习惯问题了



Windows环境下的tomcat + apache配置(绝对实践操作版)
作者 Re:这样写FOR循环的举手!! [Re:jameszhang]
dingligang





发贴: 28
积分: 0
于 2004-11-15 01:59 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Effective Java 第七章第29条(我看的是中文版)讲到了这个问题。
for和while实现的功能差不多,但是for相对while至少有2点好处:
1)for可以比while少一行

for:
for(Iterator i=c.iterator();i.hasNext() ; ){
//do sth.
}

but while
Iterator i = c.iterator();
while(i.hasNext()){
//do sth.
}


2)for可以使局部变量的作用域最小化
还是上面那个例子,Iterator i在for中,属于for的局部变量,不暴露给外面的代码,而while则不同,后续的代码可能会继续错误的使用(引用)这个变量
如:
if(i!=null){ //this i is not that i Sad, but the compiler can not detect it!!
//do sth.
}



定风波
莫听穿林打叶声,
何妨吟啸且徐行。
竹杖芒鞋轻胜马,谁怕?
一蓑烟雨任平生。
料峭春寒吹酒醒,微冷!
山头斜照却相迎。
回首向来潇洒处,归去
也无风雨也无晴。

关于 异常 。。。。 谢谢指教
作者 Re:关于FOR、WHILE循环的讨论! [Re:jameszhang]
wuyongjin





发贴: 44
积分: 0
于 2004-11-15 13:53 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
哈哈,大家 的解释都不错啊
还是看实际啊 问题时候在说啊吧!




一个程序员的奋斗历程
作者 Re:关于FOR、WHILE循环的讨论! [Re:jameszhang]
myster





发贴: 5
积分: 0
于 2004-11-19 16:46 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我个人对for循环有种亲切感。



JBuilder使用技巧
作者 Re:关于FOR、WHILE循环的讨论! [Re:jameszhang]
loverzhongping





发贴: 6
积分: 0
于 2004-11-22 16:08 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
I like use for ,and I never use while!



关于女孩子从事软件开发的问题
作者 Re:关于FOR、WHILE循环的讨论! [Re:jameszhang]
zcjl

涅槃



发贴: 537
积分: 65
于 2004-11-23 17:54 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
看不出跟楼主的代码相比:
while (true) {
if (....) {
....
break;
}
.....
}

不好在哪里?
尽管我也多数用for,而不是while

只因为 Effective Java 里的一个Item,就否决了while存在的必要
是否太极端了些?




联想游戏(欢迎跟贴,哈)
作者 Re:关于FOR、WHILE循环的讨论! [Re:jameszhang]
cicy02





发贴: 2
积分: 0
于 2005-02-18 15:45 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
用C和C++时,比较喜欢用for;
现在偏好while,尤其是do...while;




关于Jute Forum的长期发展给官方的建议

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923