首頁 > Java > java教程 > 主體

在Java中,for迴圈和增強型for迴圈之間的差異是什麼?

王林
發布: 2023-08-19 19:45:26
轉載
1457 人瀏覽過

在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 starting. genuine, the code inside the loop is executed. After each cycle, the increment/decrement step is performed.

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.

i < 5 is the condition that determines whether the loop should continue executing. As long as i is less than 5, the loop will continue iterating.

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 through value loop implementation, we add one (via ', as as value for valeverue' ' 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 have about theial spec'ein theial special special specull command - System.out.println("Iteration: " i); - outputting data comprising both text ("Iteration") and variables on-screen at present when run.

迴圈會繼續執行,直到條件 i < 5 變成假。在這種情況下,當 i 達到值為 5 時,條件不再為真,循環終止。

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 或 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 increment, initi, conitialation and conitialation and conitialation and conitialation, initialation and conitialation and conitialation and conitialation and conitialation and conitialation and conitialation and conitialation, increment and conitialation and conitialation, initialation and conitialation, increation and conitialation, increment and conitialation, initialation and conitialation, increment and conitialation, increment and conitialation, initialation, initialation, increation, initialation, increment 和steps. It is commonly utilized when the number of cycles or the particular conditions are known in development.

以上是在Java中,for迴圈和增強型for迴圈之間的差異是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板