for (Map.Entry entry : params) { //此处的FOR语句是啥意思?

nullbertauhala 2010-01-19 09:44:48


Map map = new HashMap() ;

Set<Map.Entry> params = map.entrySet() ;

for (Map.Entry entry : params) { //此处的FOR语句是啥意思?

entry.getKey() ;

entry.getValue() ;

}


...全文
291 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ineedaname 2010-01-20
  • 打赏
  • 举报
回复
for (泛型 别名 : 集合) { //此处的FOR语句是啥意思?
// 别名.他的属性
entry.getKey() ;

entry.getValue() ;

}

这里params就是一个set集合嘛
Map.Entry就是他的泛型
entry就是别名


tyt2222008 2010-01-20
  • 打赏
  • 举报
回复
就是遍历嘛,可以对一个实现了Collecion接口的对象遍历

for(Object obj: collection){
....
}
这个后面的collection可以是一个数组,也可以是一个实现了Collecion接口的对象
Z_FEI 2010-01-20
  • 打赏
  • 举报
回复
写法更简单,遍历更安全了!
hyowner 2010-01-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 focusforce 的回复:]
等价于 for (Iterator localIterator = params.iterator(); localIterator.hasNext(); )
jdk1.5之后的新特性
[/Quote]
就是这个意思
BearKin 2010-01-20
  • 打赏
  • 举报
回复
简单说下没有分 没办法 只能给他把全部信息粘贴出来了..

看样子除夕之前红星有些困难..
nihuajie05 2010-01-20
  • 打赏
  • 举报
回复
所谓的foreach操作
楼上真能贴东西啊
BearKin 2010-01-20
  • 打赏
  • 举报
回复
JDK1.5的新加的循环方式

This new language construct eliminates the drudgery and error-proneness of iterators and index variables when iterating over collections and arrays.



(语言的新结构可以消除在遍历集合和数组时迭代算子和索引变量引起的琐碎的代码和容易出错。)


The For-Each Loop(for-each循环)
Language Contents




--------------------------------------------------------------------------------

Iterating over a collection is uglier than it needs to be. Consider the following method, which takes a collection of timer tasks and cancels them:



(遍历一个集合的代码总是繁琐不堪,下面的方法展示了如何从集合里取出timer task并且取消)



void cancelAll(Collection<TimerTask> c) {

for (Iterator<TimerTask> i = c.iterator(); i.hasNext(); )

i.next().cancel();

}

The iterator is just clutter. Furthermore, it is an opportunity for error. The iterator variable occurs three times in each loop: that is two chances to get it wrong. The for-each construct gets rid of the clutter and the opportunity for error. Here is how the example looks with the for-each construct:

(遍历的代码很繁琐,而且还有出错的可能。每个循环中迭代的变量参与三次运算:这样有两个机会会出错--后两个。而for-each可以消除这种繁琐和出错可能性。下面的代码展示了for-each:)

void cancelAll(Collection<TimerTask> c) {

for (TimerTask t : c)

t.cancel();

}

When you see the colon (:) read it as “in.” The loop above reads as “for each TimerTask t in c.” As you can see, the for-each construct combines beautifully with generics. It preserves all of the type safety, while removing the remaining clutter. Because you don't have to declare the iterator, you don't have to provide a generic declaration for it. (The compiler does this for you behind your back, but you need not concern yourself with it.)

(冒号(:)读作“在”。上面的循环读作“for each TimerTask t in c.”大家可能发现到for-each和泛型的完美结合。这样即可以保证类型安全又做到了保持代码简洁。因为你不必定义个迭代变量,不用定义一个泛型的迭代变量。注:实际上编译器完成这个动作,不用你亲自为之。)

Here is a common mistake people make when they are trying to do nested iteration over two collections:

(下面是大家在嵌套迭代两个结合时容易犯的错误:)

List suits = ...;

List ranks = ...;

List sortedDeck = new ArrayList();



// BROKEN - throws NoSuchElementException!

for (Iterator i = suits.iterator(); i.hasNext(); )

for (Iterator j = ranks.iterator(); j.hasNext(); )

sortedDeck.add(new Card(i.next(), j.next()));

Can you spot the bug? Don't feel bad if you can't. Many expert programmers have made this mistake at one time or another. The problem is that the next method is being called too many times on the “outer” collection (suits). It is being called in the inner loop for both the outer and inner collections, which is wrong. In order to fix it, you have to add a variable in the scope of the outer loop to hold the suit:

(有没有发现bug?如果没有也不用难为情。很多专业的程序员会时不时的犯这样的错误。问题出在外循环的suits调用了多余的next方法,在内层循环调用了外面的和里面的两个集合。要改正它,你得在外循环的作用域定义个变量保存suit当前值。)

// Fixed, though a bit ugly

for (Iterator i = suits.iterator(); i.hasNext(); ) {

Suit suit = (Suit) i.next();

for (Iterator j = ranks.iterator(); j.hasNext(); )

sortedDeck.add(new Card(suit, j.next()));

}

So what does all this have to do with the for-each construct? It is tailor-made for nested iteration! Feast your eyes:

(这些和for-each有什么联系吗?for-each可以说是为嵌套迭代量身定做的!大饱眼福吧:)



for (Suit suit : suits)

for (Rank rank : ranks)

sortedDeck.add(new Card(suit, rank));

The for-each construct is also applicable to arrays, where it hides the index variable rather than the iterator. The following method returns the sum of the values in an int array:

(for-each同样可以用于数组,这时候隐藏了索引变量而不是迭代变量。下面的代码返回int型数组的和:)

// Returns the sum of the elements of a

int sum(int[] a) {

int result = 0;

for (int i : a)

result += i;

return result;

}

So when should you use the for-each loop? Any time you can. It really beautifies your code. Unfortunately, you cannot use it everywhere. Consider, for example, the expurgate method. The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator, so you cannot call remove. Therefore, the for-each loop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it. Finally, it is not usable for loops that must iterate over multiple collections in parallel. These shortcomings were known by the designers, who made a conscious decision to go with a clean, simple construct that would cover the great majority of cases.

(那么什么适合使用for-each循环呢?你可以随意为之。它可以是您的代码大为美观。不过你可不能什么地方都用。回想下expurgate方法。那里需要访问迭代变量并删除当前元素。而for-each隐藏了迭代变量,这样就无法实现删除。因此,for-each循环不适合实现过滤功能,同时也不适合在循环中替换集合和数组中的元素,最后在并行迭代多重集合的循环中也不可使用。这些缺点在设计之初可以通过有意识的大范围的设计干净、简单的结构加以避免。)
mynameissunli 2010-01-20
  • 打赏
  • 举报
回复
学习……
jypapgl 2010-01-19
  • 打赏
  • 举报
回复
jdk1.5的新循环 无法控制的 没啥好的 就是写着方便
qx8668 2010-01-19
  • 打赏
  • 举报
回复
增强的循环。。。
focusforce 2010-01-19
  • 打赏
  • 举报
回复
等价于 for (Iterator localIterator = params.iterator(); localIterator.hasNext(); )
jdk1.5之后的新特性
marco2000 2010-01-19
  • 打赏
  • 举报
回复
crazylaa 2010-01-19
  • 打赏
  • 举报
回复
-->
jdk1.5新特性。
mwyking 2010-01-19
  • 打赏
  • 举报
回复
好东西,大家一起学习。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧