先是说for比while更安全,因为for是把一推东西都写在括号中的嘛.
而while需要现在外部定义一个对象,
所以,while中可能出现如此代码,
Iterator<Element> i = c.iterator();
while(i.hasNext()){
doSomething(i.next());
}
//...
Iterator<Element> i2 = c2.iterator();
while(i.hasNext()){//这里有bug
doSomething(i2.next());
}
for循环就不可能出现.
然后,就是一个比较有意思的,可能你知道,算俺少见多怪吧...
for (int i = 0, j = 1, k = 2;;) {
//我认为可以定义很多变量也很有意思...
}
-------------------
"利用for-each循环不会有性能损失,甚至用于数组也一样。实际上,在某些情况下,比起普通的for循环,它还稍有些性能优势,因为它对数组索引的边界值只计算一次。"
public static void main(String[] args) {
int[] iArr = new int[10000000];
long forBegin = System.currentTimeMillis();
for (int i = 0; i < iArr.length; i++) {
test(iArr[i]);
}
long forEnd = System.currentTimeMillis();
for (int i : iArr) {
test(i);
}
long foreach = System.currentTimeMillis();
System.out.println("for:" + (forEnd - forBegin) + " foreach:" + (foreach - forEnd));
//输出结果,for:31 foreach:16
}
public static void test(int i) {
}
看输出结果,果不其然!for-each是怎么实现的呢?看来肯定不是用普通for循环鸟...
-------------------
在看一个,书中要讲明要用for-each,尽量不要用for的一个例子,for可能会遇到的一个问题,
public static void main(String[] args) {
List<Suit> suits = Arrays.asList(Suit.values());
List<Rank> ranks = Arrays.asList(Rank.values());
List<Card> deck = new ArrayList<Card>();
for (Iterator<Suit> i = suits.iterator(); i.hasNext();) {
for (Iterator<Rank> j = ranks.iterator(); j.hasNext();) {
deck.add(new Card(i.next(), j.next()));
//问题就在于这里的i.next(),多次的j循环,i也是不断next()的呀...
}
}
}
enum Suit {
CLUB, DIAMOND, HEART, SPADE
}
enum Rank {
ACE, // [eis] 幺点
DEUCE, // [dju:s] 两点
THREE, FOUR, FIVE, SIX, SEVEN, NINE, TEN, JACK, QUEEN, KING
}
当然,解决办法简单到只需在第二个for循环前把i.next()赋给一个对象.
但是,<effective java>曰不优雅呀!
okay,于是乎就这样了,
for (Suit suit : Suits)
for (Rank rank : ranks)
deck.add(new Card(suit,rank));
要注意呀,"for-each循环不仅让你遍历集合和数组,还让你遍历任何实现Iterable接口的对象",O耶,以后要玩集合都要实现Iterable鸟...
-------------------
至于最后提单的,"过滤"和"转换"时不要用for-each并非没有瑕疵.如果是个对象的集合的话,用for-each做remove(过滤)和为属性赋值(转换)应该也是木问题的.
当然,如果是简单类型集合的话,"转换"与"过滤"不宜用for-each了.("转换",如果使用=或者new的话,即使是对象集合,也是不行的吧)
分享到:
相关推荐
one step at a time<br/><br/>Understand the Executable and Linking Format (ELF), and use that knowledge for more effective debugging<br/><br/>Includes a production-ready data collection script that ...
<div class="block">An example class demonstrating basic Java concepts.</div> </td> </tr> </tbody> </table> </div> </section> </div> #### 五、相关文档 - [Effective Java]...
"Effective Java读书笔记" Effective Java是一本关于Java编程语言的经典书籍,本笔记主要总结了Java语言的发展历程、静态工厂方法的应用、构造器模式的使用等重要知识点。 一、Java语言的发展历程 Java语言的发展...
<book title="Effective Java"> <author> Joshua Bloch </author> <year>2001</year> </book> </category> <category name="Movies"> <!-- ... --> </category> </root> ``` 使用JDOM解析这样的文件,可以...
《More Effective C++》是C++编程领域的一本经典书籍,由Scott Meyers撰写,它为程序员提供了35个实用的技巧和最佳实践,旨在帮助他们编写更高效、更易于维护的C++代码。这本书是《Effective C++》系列的一部分,...
<br><br>《Effective STL》目录:<br><br>前言<br>致谢<br>导读<br>容器<br>条款1:仔细选择你的容器<br>条款2:小心对“容器无关代码”的幻想<br>条款3:使容器里对象的拷贝操作轻量而正确<br>条款4:用empty来代替...
Fast, easy, effective way to manipulate INI files.<END><br>55,SoundSource.zip<br>BrainFusion Presents The Dancer.Ocx Which is capable of reading The Sound Peak Form Sound Card When Mp3 or Wav File Is...
<br/>Publisher: Sams Publishing <br/>Pub Date: July 26, 2005 <br/>ISBN: 0-672-32798-8 <br/>Pages: 408 <br/> <br/>* Overview *<br/>Software Testing, Second Edition provides practical insight into the ...
<<effective c++>>这本圣经的作者Scott Meyesr在给<<modern c++ design>>序言的时候高度的赞赏了Andrei同志的工作:C++社群对template的理解即将经历一次巨大的变化,我对它所说的任何事情,也许很快就会被认为是...
Credits<br> About the Author<br> Contributors<br> Acknowledgments<br> ... Run Native Java Applications<br> Section 98. Rotate Your Signature<br> Section 99. Useful One-Liners<br> Section 9.13. Fun with X
B<br>DESCRIPTION:<br>US=HP Windows Format Utility for USB Drive Key or DiskOnKey<br><br>PURPOSE: Initial Release<br>SOFTPAQ NUMBER: SP27213<br>EFFECTIVE DATE: February 6, 2004<br>CATEGORY: ...
<<effective c++>>这本圣经的作者Scott Meyesr在给<<modern c++ design>>序言的时候高度的赞赏了Andrei同志的工作:C++社群对template的理解即将经历一次巨大的变化,我对它所说的任何事情,也许很快就会被认为是...
<tr><td>1</td><td>2</td><td>3</td><td>4</td></tr> <tr><td>5</td><td>6</td><td>7</td><td>8</td></tr> <tr><td>9</td><td>10</td></tr> </table> ``` 5. **个人兴趣**: 面试官可能会问及你的学习资源,...
This book also includes an insightful use-case primer that will be indispensable both to novices and veteran modelers seeking to become more effective.<br><br>Coverage includes<br><br>Modeling rules ...
Effective Use of Microsoft Enterprise Library is that roadmap.<br/><br/>Microsoft application development lead architect Len Fenster explains exactly how to build applications using Enterprise ...
interoperability, and more <br><br>Running case study: see how concepts work together when you build .NET applications <br><br>Experienced C++, Java, and Visual Basic programmers: become effective ...
Effective Use of Microsoft Enterprise Library is that roadmap.<br/><br/>Microsoft application development lead architect Len Fenster explains exactly how to build applications using Enterprise ...
《Effective Java》是Java开发领域的经典著作,作者Joshua Bloch深入浅出地阐述了编写高效、健壮的Java代码的技巧和最佳实践。以下是对该书部分内容的详细解释: 1. **产生和销毁对象** - Item1:静态工厂方法相比...
<br>The Business Case For E-Learning<br><br>by Tom M Kelly , Nader A Nanjiani <br><br>Paperback: 216 pages <br>Publisher: Cisco Press (October 29, 2004) <br>Language: English <br>ISBN-10: 1587200864 ...
<title>Effective Java</title> <author>杰伊·格洛斯曼</author> <year>2001</year> </book> </library> ``` 我们可以创建对应的Java类来映射XML元素: ```java public class Book { @Attribute(name = "id...