0

0

在Java中,for循环和增强型for循环之间的区别是什么?

王林

王林

发布时间:2023-08-19 19:45:26

|

1846人浏览过

|

来源于tutorialspoint

转载

在java中,for循环和增强型for循环之间的区别是什么?

当涉及到迭代元素时,Java提供了许多选择,其中两个流行的循环结构是传统的和增强的“for each”循环,它们分别提供了不同的方法来完成这个任务。了解这些机制的差异是Java程序员在特定情况下选择最适合的样式的重要信息。

语法

The syntax of the traditional for loop is as follows:

for (initialization; condition; increment/decrement) {
   // Code to be executed
}

增强型for循环,也被称为“foreach”循环,具有不同的语法:

for (datatype variable : collection) {
   // Code to be executed
}

语法解释

The conventional for loop consists of three parts: initialization, condition, and increment/decrement. The initialization step is executed as it were once at the starting. The condition is evaluated before each cycle, and on the off chance that it is genuine, the code inside the loop is executed. After each cycle, the increment/decrement step is performed.

立即学习Java免费学习笔记(深入)”;

On the other hand, the improved for loop simplifies the language structure by eliminating the requirement for initialization, condition, and increment/decrement steps. It directly iterates over a collection or array.

Approach 1: Traditional for loop

Algorithm

  • Initialize a variable.

  • Specify the condition for executing the loop.

  • Execute the code block inside the loop.

  • Increment or decrement the variable.

Example

的中文翻译为:

示例

public class TraditionalForLoopExample {
   public static void main(String[] args) {
      for (int i = 0; i < 5; i++) {
         System.out.println("Iteration: " + i);
      }
   }
}

Output

Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4

Explanation

的中文翻译为:

解释

代码以声明一个名为TraditionalForLoopExample的公共类开始

在课堂的范围内,人们可以发现一个被称为主方法的基本过程。这个组件作为程序开始执行的入口。

The for keyword indicates the start of the loop construct.

int i = 0 initializes a loop control variable i with an initial value of 0.

造梦阁AI
造梦阁AI

AI小说推文一键成片,你的故事值得被看见

下载

i

This code employs an iteration statement in order to update an incrementing integer variable named 'i'. In each subsequent cycle through our program loop implementation, we add one (via '++', as mentioned) to whatever present value for 'i' we encounter via our command stream here- allowing us to track current iterators with ease. Contained within a block cited via brackets {}, we have everything that comes under our programmatic umbrella when we talk about 'the loop.' Herein lies a special command - System.out.println("Iteration: " + i); - outputting data comprising both text ("Iteration") and variables on-screen at present when run.

循环会继续执行,直到条件 i

Approach 2: Enhanced for loop

Algorithm

  • 声明一个变量来保存集合中的每个元素。

  • Specify the collection to be iterated.

  • 在循环中执行代码块,使用声明的变量访问每个元素。

  • Consider the following example of the enhanced for loop

Example

的中文翻译为:

示例

public class EnhancedForLoopExample {
   public static void main(String[] args) {
      String[] fruits = {"Apple", "Banana", "Orange"};
      for (String fruit : fruits) {
         System.out.println("Fruit: " + fruit);
      }
   }
}

Output

Fruit: Apple
Fruit: Banana
Fruit: Orange

Explanation

的中文翻译为:

解释

代码以声明一个名为EnhancedForLoopExample的公共类开始。

在课堂的范围内,人们可以发现一个被称为主方法的基本过程。这个组件作为程序开始执行的入口。

声明了一个名为fruits的String类型的数组。这行代码创建了一个名为fruits的数组,可以存储String值。该数组被初始化为三个元素:"Apple","Banana"和"Orange"。

The enhanced for loop simplifies the process of iterating over arrays and collections.

循环遍历水果数组中的每个元素,将当前元素赋值给循环变量fruit。

对于每次迭代,执行用花括号{}括起来的代码块,可以轻松地打印出水果数组中的每个单独元素。输出包括一个静态标签“Fruit:”和一个表示当前迭代过程中任意特定项的变量值,通过System.out.println("Fruit: " + fruit);。这种方法消除了与手动索引技术常用于遍历数组等数据集相关的顺序错位或索引间隙的风险。

Difference Between for loop and Enhanced for loop in Java

差异点

Traditional for Loop

增强型for循环

Syntax

Requires explicit initialization, condition, and increment/decrement steps

简化语法,无需初始化、条件或增减步骤

Iteration Control

提供了更多对初始化、条件和增量/减量步骤的控制

自动迭代集合或数组的元素

访问元素

Can access elements using an index variable and array/collection size

直接访问元素,无需索引或大小

代码可读性

Requires explicit handling of iteration details

通过抽象迭代细节来提高代码可读性

Use Cases

Suitable for situations where explicit control over iteration is necessary

Ideal for iterating over collections or arrays without complex iteration requirements

Conclusion

Both the traditional for loop and the enhanced for loop have their own significance in Java programming. The conventional for loop gives more adaptability and control over the emphasis handle, permitting the software engineer to characterize the initialization, condition, and increment/decrement steps. It is commonly utilized when the number of cycles or the particular conditions are known in development.

相关专题

更多
java
java

Java是一个通用术语,用于表示Java软件及其组件,包括“Java运行时环境 (JRE)”、“Java虚拟机 (JVM)”以及“插件”。php中文网还为大家带了Java相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

832

2023.06.15

java正则表达式语法
java正则表达式语法

java正则表达式语法是一种模式匹配工具,它非常有用,可以在处理文本和字符串时快速地查找、替换、验证和提取特定的模式和数据。本专题提供java正则表达式语法的相关文章、下载和专题,供大家免费下载体验。

738

2023.07.05

java自学难吗
java自学难吗

Java自学并不难。Java语言相对于其他一些编程语言而言,有着较为简洁和易读的语法,本专题为大家提供java自学难吗相关的文章,大家可以免费体验。

734

2023.07.31

java配置jdk环境变量
java配置jdk环境变量

Java是一种广泛使用的高级编程语言,用于开发各种类型的应用程序。为了能够在计算机上正确运行和编译Java代码,需要正确配置Java Development Kit(JDK)环境变量。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

397

2023.08.01

java保留两位小数
java保留两位小数

Java是一种广泛应用于编程领域的高级编程语言。在Java中,保留两位小数是指在进行数值计算或输出时,限制小数部分只有两位有效数字,并将多余的位数进行四舍五入或截取。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

398

2023.08.02

java基本数据类型
java基本数据类型

java基本数据类型有:1、byte;2、short;3、int;4、long;5、float;6、double;7、char;8、boolean。本专题为大家提供java基本数据类型的相关的文章、下载、课程内容,供大家免费下载体验。

446

2023.08.02

java有什么用
java有什么用

java可以开发应用程序、移动应用、Web应用、企业级应用、嵌入式系统等方面。本专题为大家提供java有什么用的相关的文章、下载、课程内容,供大家免费下载体验。

430

2023.08.02

java在线网站
java在线网站

Java在线网站是指提供Java编程学习、实践和交流平台的网络服务。近年来,随着Java语言在软件开发领域的广泛应用,越来越多的人对Java编程感兴趣,并希望能够通过在线网站来学习和提高自己的Java编程技能。php中文网给大家带来了相关的视频、教程以及文章,欢迎大家前来学习阅读和下载。

16925

2023.08.03

Golang gRPC 服务开发与Protobuf实战
Golang gRPC 服务开发与Protobuf实战

本专题系统讲解 Golang 在 gRPC 服务开发中的完整实践,涵盖 Protobuf 定义与代码生成、gRPC 服务端与客户端实现、流式 RPC(Unary/Server/Client/Bidirectional)、错误处理、拦截器、中间件以及与 HTTP/REST 的对接方案。通过实际案例,帮助学习者掌握 使用 Go 构建高性能、强类型、可扩展的 RPC 服务体系,适用于微服务与内部系统通信场景。

6

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 0.9万人学习

光速学会docker容器
光速学会docker容器

共33课时 | 1.9万人学习

时间管理,自律给我自由
时间管理,自律给我自由

共5课时 | 0.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号