1. The for-each loop, introduced in release 1.5, gets rid of the clutter and the opportunity for error by hiding the iterator or index variable completely. The resulting idiom applies equally to collections and arrays:
// The preferred idiom for iterating over collections and arrays
for (Element e : elements) {
doSomething(e);
}
2. Not only does the for-each loop let you iterate over collections and arrays, it lets you iterate over any object that implements the Iterable interface. If you are writing a type that represents a group of elements, have it implement Iterable even if you choose not to have it implement Collection. This will allow your users to iterate over your type using the for-each loop.
3. There are three common situations where you can’t use a for-each loop:
1) Filtering—If you need to traverse a collection and remove selected elements, then you need to use an explicit iterator so that you can call its remove method.
2) Transforming—If you need to traverse a list or array and replace some or all of the values of its elements, then you need the list iterator or array index in order to set the value of an element.
3) Parallel iteration—If you need to traverse multiple collections in parallel, then you need explicit control over the iterator or index variable, so that all iterators or index variables can be advanced in lockstep.
相关推荐
Item 58: Prefer for-each loops to traditional for loops Item 59: Know and use the libraries Item 60: Avoid float and double if exact answers are required Item 61: Prefer primitive types to boxed ...
- `prefer`: 更喜欢,宁愿 - `suffer`: 遭受,忍受 - `apply`: 申请,应用 - `contribute`: 捐献,贡献 - `serve`: 招待,服务 - `depend`: 依赖,依靠 - `affect`: 影响,侵袭 - `offer`: 提供,出价 - `...
- It’s hard for me to learn to play Chinese chess. 翻译:对我来说学习下中国象棋很难。 分析:这个句型结构是"It + be + 形容词 + for sb. to do sth.",表示某人做某事的难度或特点。 练习题答案: a. It’...
- `prefer to do sth rather than do sth`: 宁愿做某事而不愿做另一件事。 - `would rather do sth`: 宁愿做某事,常用于表达现在或未来的意愿。 - `一般过去时表示现在的愿望或过去的愿望`: 如"I'd rather you ...
- `be ready for = get ready for = prepare for`:为……作准备 - `be able to = can`:能够(后接动词原形,be有各种形式,can只有时态变化) - `have fun = have a good time = enjoy oneself`:玩得高兴...
- **七月末我将会离开,我将要在那里待四个星期**:I'm leaving at the end of July and I'm going to stay there for four weeks. - **顺便说一下,别忘记带你的护照**:Oh, by the way, don't forget to take ...
- 用`prefer...to...`改写:I prefer red apples to green ones. - 用`would rather...than...`改写:I would rather walk than ride a bike. - 与橘子相比,她更喜欢苹果:She prefers apples to oranges. - 我...
- 例句:Each item on the list is important. #### 必修1 Unit2 58. **subway** (n.) - 含义:地铁 - 例句:We took the subway to the city center. 59. **elevator** (n.) - 含义:电梯 - 例句:The ...
- `prefer…to…`:比起...更喜欢... - `in that case`:在这种情况下 - `stick to`:坚持 - `plenty of`:许多 - `shut off`:关闭 - `once in a while`:偶尔 - `look up`:抬头看,查阅 - `during one’s...
**Item 8: Prefer Query Syntax to Loops** - **Advantages:** Query syntax is more readable and expressive than traditional loops. - **Example:** ```csharp var query = from p in people where p.Age >...
Which fast food restaurant do you prefer? - 3. 我希望你能来参加我的生日派对。I'd like you to come to my birthday party. - 4. 商店里不再有洋娃娃了。There aren't any dolls in the shop anymore. - 5. ...
- It's a pity that only six pieces were recorded for future generations to hear, but his fame lives on. 这些内容旨在帮助学生理解和运用与音乐相关的英语表达,同时提升他们在听力、口语、阅读和写作方面的...
- 动词搭配:"prefer A to B" 表示"更喜欢A而不是B",此处应选C.prefer; to - 现在完成时的使用:"Half a month has passed…",应选C.has passed - 形容词与副词的区分:"alone" 通常作副词,"lonely" 有孤独的...
- prefer → preference (n.):偏爱;preferred (过去式/过去分词):更喜欢的 - shut → shut (过去式/过去分词):关上 - reflect → reflection (n.):反映;reflective (adj.):反射的;反映的 - change → ...
- 更喜欢做某事:prefer to do sth. - 宁愿某人做某事:would rather sb. do sth. - 喜欢A而不喜欢B:like A better than B - 宁愿做...而不愿做...:would rather do... than do... - 感觉想做某事:feel like...
- `prefer to do sth rather than do sth`:宁愿做...而不愿做... - `prefer doing sth to doing sth`:比起...更喜欢... - `would rather do sth than do sth`:宁愿做...而不愿做... - `would do sth rather ...
--tee : mirror output to a filename specified after '--tee' -4 : prefer ipv4 -6 : prefer ipv6 -c : only show an output line on changed state --file : treat the "server-address" as a filename ...
2. 题目3:动名词 - "John enjoys jogging while his brother prefers going skiing." "enjoy doing" 结构表示喜欢做某事,"prefer to do" 或 "prefer doing" 表示更喜欢做某事。 3. 题目4:固定搭配 - "I'd rather...
- **扩展练习:** "I need to book a sleeper compartment for the night train to New York." #### 4. 这件行李我不托运。 **I won't check this baggage.** - **应用场景:** 在办理登机手续时使用。 - **表达...
composer create-project --prefer-dist cakephp/app 如果您想使用自定义应用程序目录名称(例如/myapp/ ): composer create-project --prefer-dist cakephp/app myapp 现在,您可以使用计算机的Web服务器查看...