会挽雕弓如满月,西北望,射天狼。 注册 | 登陆

Java学习日记:for循环的新用法

Java为其for循环实现了针对数组和集合类的新用法:
The for statement also has another form designed
for iteration through Collections and arrays This form is
sometimes referred to as the enhanced for statement,
and can be used to make your loops more compact and
easy to read. To demonstrate, consider the following array,
which holds the numbers 1 through 10:


int[] numbers = {1,2,3,4,5,6,7,8,9,10};

The following program, EnhancedForDemo, uses the enhanced
for to loop through the array:


class EnhancedForDemo {
public static void main(String[] args){
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println("Count is: " + item);
}
}
}

In this example, the variable item holds the current value from
the numbers array. The output from this program is the same
as before:
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10

We recommend using this form of the for statement instead of
the general form whenever possible.

Tags: 编程, Java

« 上一篇 | 下一篇 »

只显示5条记录相关文章

Usereg Tunet for Android (浏览: 553, 评论: 0)
可能是jQuery1.3.2的一个小bug (浏览: 2662, 评论: 0)
扫雷对战版 (浏览: 13873, 评论: 1)
一个用Qt写的多线程聊天室软件 (浏览: 5764, 评论: 3)
C/C++利用CPU时钟计数器精确计时 (浏览: 4119, 评论: 0)

发表评论

评论内容 (必填):