Java 实例 - for 和 foreach循环使用

Java 教程 · 2019-02-10 17:58:25

for 语句比较简单,用于循环数据。

for循环执行的次数是在执行前就确定的。语法格式如下:

for(初始化; 布尔表达式; 更新) {
    //代码语句
}

foreach语句是java5的新特征之一,在遍历数组、集合方面,foreach为开发人员提供了极大的方便。

foreach 语法格式如下:

for(元素类型t 元素变量x : 遍历对象obj){ 
     引用了x的java语句; 
} 

以下实例演示了 for 和 foreach循环使用:

Main.java 文件

public class Main { public static void main(String[] args) { int[] intary = { 1,2,3,4}; forDisplay(intary); foreachDisplay(intary); } public static void forDisplay(int[] a){ System.out.println("使用 for 循环数组"); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } public static void foreachDisplay(int[] data){ System.out.println("使用 foreach 循环数组"); for (int a : data) { System.out.print(a+ " "); } } }

以上代码运行输出结果为:

使用 for 循环数组
1 2 3 4 
使用 foreach 循环数组
1 2 3 4

点击查看所有 Java 教程 文章: https://www.codercto.com/courses/l/12.html

查看所有标签

Flexible Rails

Flexible Rails

Peter Armstrong / Manning Publications / 2008-01-23 / USD 44.99

Rails is a fantastic tool for web application development, but its Ajax-driven interfaces stop short of the richness you gain with a tool like Adobe Flex. Simply put, Flex is the most productive way t......一起来看看 《Flexible Rails》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具