本文介紹了使用 for 循環(huán)或 while 循環(huán)進(jìn)行迭代?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我經(jīng)常看到這樣的代碼:
I often see code like:
Iterator i = list.iterator();
while(i.hasNext()) {
...
}
但我這樣寫(當(dāng) Java 1.5 不可用或每個(gè)都不能使用時(shí)):
but I write that (when Java 1.5 isn't available or for each can't be used) as:
for(Iterator i = list.iterator(); i.hasNext(); ) {
...
}
因?yàn)?/p>
- 更短了
- 它將
i
保持在較小的范圍內(nèi) - 它減少了混淆的機(jī)會(huì).(
i
是否在盡管?i
在哪里聲明?)
- It is shorter
- It keeps
i
in a smaller scope - It reduces the chance of confusion. (Is
i
used outside the while? Where isi
declared?)
我認(rèn)為代碼應(yīng)該盡可能簡(jiǎn)單易懂,這樣我只需要編寫復(fù)雜的代碼來(lái)完成復(fù)雜的事情.你怎么認(rèn)為?哪個(gè)更好?
I think code should be as simple to understand as possible so that I only have to make complex code to do complex things. What do you think? Which is better?
來(lái)自:http://jamesjava.blogspot.com/2006/04/iterating.html
推薦答案
我更喜歡 for 循環(huán),因?yàn)樗€將迭代器的范圍設(shè)置為僅 for 循環(huán).
I prefer the for loop because it also sets the scope of the iterator to just the for loop.
這篇關(guān)于使用 for 循環(huán)或 while 循環(huán)進(jìn)行迭代?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!