l语法:
Øfor ( type 变量名:集合变量名 ) { … }
l注意事项:
Ø迭代变量必须在( )中定义!
Ø集合变量可以是数组或实现了Iterable接口的集合类
l举例:
public static int add(int x,int ...args) {
int sum = x;
for(int arg:args) {
sum += arg;
}
return sum;
}
The enhanced for statement has the form:
EnhancedForStatement:
for ( Var iableModi f ie rsopt
Type Identifier: Expression) Statement
The Expression must either have type Iterable or else it must be of an array
type (§10.1), or a compile-time error occurs.
The scope of a local variable declared in the FormalParameter part of an
enhanced for statement (§14.14) is the contained Statement
The meaning of the enhanced for statement is given by translation into a
basic for statement.
If the type of Expression is a subtype of Iterable, then let I be the type of the
expression Expression.iterator(). The enhanced for statement is equivalent to
a basic for statement of the form:
for (I #i = Expression.iterator(); #i.hasNext(); ) {
VariableModifiersopt Type Identifier = #i.next();
Statement
}
Where #i is a compiler-generated identifier that is distinct from any other identifi-
ers (compiler-generated or otherwise) that are in scope (§6.3) at the point where
the enhanced for statement occurs.
Otherwise, the Expression necessarily has an array type, T[]. Let L1 ... Lm
be the (possibly empty) sequence of labels immediately preceding the enhanced
for statement. Then the meaning of the enhanced for statement is given by the
following basic for statement:
T[] a = Expression;
L1: L2: ... Lm:
for (int i = 0; i < a.length; i++) {
VariableModifiersopt
Type Identifier = a[i];
Statement
}
Where a and i are compiler-generated identifiers that are distinct from any other
identifiers (compiler-generated or otherwise) that are in scope at the point where
the enhanced for statement occurs.
DISCUSSION
The following example, which calculates the sum of an integer array, shows how enhanced
for works for arrays:
int sum(int[] a) {
int sum = 0;
for (int i : a)
sum += i;
return sum;
}
Here is an example that combines the enhanced for statement with auto-unboxing to trans-
late a histogram into a frequency table:
Map<String, Integer> histogram = ...;
double total = 0;
for (int i : histogram.values())
total += i;
for (Map.Entry<String, Integer> e : histogram.entrySet())
System.out.println(e.getKey() + "" + e.getValue() / total);
分享到:
相关推荐
增强的for循环是在传统的for循环中增加的强大的迭代功能的循环,是在jdk1.5之后提出来的。下面这篇文章主要给大家介绍了关于Java中增强for循环的实现原理和坑的相关资料,需要的朋友可以参考下
NULL 博文链接:https://rensanning.iteye.com/blog/2003205
java for循环与增强for循环.docx
增强型的for循环 和普通for循环一样 增强型的for循环 优点主要体现在集合中,随便举个例子 比如对 set 的遍历 一般是迭代遍历
J2SE for Loop循环 增强for循环 Enhanced for Loop 更简单的进行遍历
JDK1.5新特性中的最重要的一点。让你彻底明白增强for循环的强大和方便
java代码-使用java增强for循环 string数组赋值的源代码 ——学习参考资料:仅用于个人学习使用!
JDK5增强for循环的使用,与以前的for循环保额不同!
下面小编就为大家带来一篇老生常谈foreach(增强for循环)和for的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
18-集合(增强for循环).avi
超全面javaweb教程第7天-_14_增强for循环
下面小编就为大家带来一篇java增强for循环的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
【IT十八掌徐培成】Java基础第12天-04.集合工具类-增强for循环-变长参数.zip
foreach 是 Java 中的一种语法糖,几乎每一种语言都有一些这样的语法糖来方便程序员进行开发,编译期间以特定的字节码或特定的方式来对这些语法进行处理。能够提高性能,并减少代码出错的几率。
下面小编就为大家带来一篇Java中增强for循环在一维数组和二维数组中的使用方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
在本篇文章里小编给大家整理是的关于Java中如何使用增强for循环的实例内容以及相关代码,需要的朋友们可以学习下。
/*公司财务发工资时,记录了当时发工资的资料Employee.txt 1.定义公司员工类Employee,属性有:工号,姓名,性别,工资(double类型),进行属性的隐藏和封装,重写toString. 2.将D:/Employee.txt文件用程序复制到项目根目录;...
Java增强的for循环讲解Java增强的for循环讲解
主要介绍了详解Java中list,set,map的遍历与增强for循环的相关资料,需要的朋友可以参考下