本文介紹了使用 for 循環或 while 循環進行迭代?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我經常看到這樣的代碼:
I often see code like:
Iterator i = list.iterator();
while(i.hasNext()) {
...
}
但我這樣寫(當 Java 1.5 不可用或每個都不能使用時):
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(); ) {
...
}
因為
- 更短了
- 它將
i
保持在較小的范圍內 - 它減少了混淆的機會.(
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?)
我認為代碼應該盡可能簡單易懂,這樣我只需要編寫復雜的代碼來完成復雜的事情.你怎么認為?哪個更好?
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?
來自:http://jamesjava.blogspot.com/2006/04/iterating.html
推薦答案
我更喜歡 for 循環,因為它還將迭代器的范圍設置為僅 for 循環.
I prefer the for loop because it also sets the scope of the iterator to just the for loop.
這篇關于使用 for 循環或 while 循環進行迭代?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!