Enhanced for Loop (For-Each Loop) in Java – जावा में Enhanced for Loop
The Enhanced for loop in Java is also known as the for-each loop. It is used to traverse or iterate through the elements of an array or a collection without manually managing an index or iterator. It provides a simple and readable way to access each element one by one. The enhanced for loop was introduced in Java 5 and is particularly useful when we need to process all elements of an array or collection.
Java में Enhanced for Loop को For-Each Loop भी कहा जाता है। इसका उपयोग Array या Collection के सभी Elements को एक-एक करके Access करने के लिए किया जाता है। इसमें Index या Iterator को स्वयं नियंत्रित करने की आवश्यकता नहीं होती। यह प्रत्येक Element को सरल और पठनीय तरीके से प्राप्त करने की सुविधा प्रदान करता है। Enhanced for Loop को Java 5 में प्रस्तुत किया गया था और इसका उपयोग विशेष रूप से तब किया जाता है, जब Array या Collection के सभी Elements पर कार्य करना हो।
Syntax of Enhanced for Loop (Enhanced for Loop का सिंटैक्स)
The general syntax of the enhanced for loop is:
Enhanced for Loop का सामान्य सिंटैक्स निम्नलिखित है:
for (dataType variable : arrayOrCollection) {// Statements to be executed}
Description (विवरण):
dataType: Specifies the data type of the elements.variable: Stores the current element during each iteration.arrayOrCollection: Specifies the array or collection to be traversed.: The colon symbol separates the loop variable from the array or collection.
dataType: यह Elements के Data Type को निर्धारित करता है।variable: प्रत्येक Iteration में वर्तमान Element को Store करता है।arrayOrCollection: उस Array या Collection को दर्शाता है जिसके Elements पर कार्य करना है।:: Colon Symbol Loop Variable और Array या Collection को अलग करता है।
1. Enhanced for Loop with Array (Array के साथ Enhanced for Loop)
The enhanced for loop is commonly used to access all elements of an array. It automatically retrieves each element one by one, starting from the first element and continuing to the last element.
Enhanced for Loop का उपयोग सामान्यतः Array के सभी Elements को Access करने के लिए किया जाता है। यह पहले Element से शुरू होकर अंतिम Element तक प्रत्येक Element को अपने आप एक-एक करके प्राप्त करता है।
Java Program: Display Array Elements (Array के Elements प्रदर्शित करना)
public class ForEachArray {public static void main(String[] args) {int numbers[] = {10, 20, 30, 40, 50};System.out.println("Array Elements:");for (int num : numbers) {System.out.println(num);}}}
Output:
Array Elements:1020304050
Explanation (व्याख्या):
In this program, an integer array named numbers contains five elements. The enhanced for loop accesses each element and stores its value in the variable num. The loop executes once for each element and displays its value.
इस प्रोग्राम में numbers नाम का एक Integer Array है, जिसमें पाँच Elements हैं। Enhanced for Loop प्रत्येक Element को Access करके उसका मान num वेरिएबल में Store करता है। यह Loop प्रत्येक Element के लिए एक बार निष्पादित होता है और उसका मान प्रदर्शित करता है।
2. Enhanced for Loop with String Array (String Array के साथ Enhanced for Loop)
The enhanced for loop can also be used with String arrays. It retrieves each String element from the array without requiring an index.
Enhanced for Loop का उपयोग String Array के साथ भी किया जा सकता है। यह Index का उपयोग किए बिना Array के प्रत्येक String Element को प्राप्त करता है।
Java Program: Display Names Using Enhanced for Loop (Enhanced for Loop से नाम प्रदर्शित करना)
public class ForEachString {public static void main(String[] args) {String names[] = {"Amit", "Rahul", "Neha", "Priya"};System.out.println("Student Names:");for (String name : names) {System.out.println(name);}}}
Output:
Student Names:AmitRahulNehaPriya
Explanation (व्याख्या):
In this program, the names array contains four String elements. The enhanced for loop retrieves each name and stores it in the name variable. Each name is then displayed using System.out.println().
इस प्रोग्राम में names Array में चार String Elements हैं। Enhanced for Loop प्रत्येक नाम को प्राप्त करके name वेरिएबल में Store करता है। इसके बाद प्रत्येक नाम को System.out.println() की सहायता से प्रदर्शित किया जाता है।
3. Enhanced for Loop with Collection (Collection के साथ Enhanced for Loop)
The enhanced for loop can also be used to iterate through Java collections, such as ArrayList, LinkedList, and HashSet. It provides a convenient way to process collection elements without explicitly using an iterator.
Enhanced for Loop का उपयोग Java Collections जैसे ArrayList, LinkedList और HashSet के Elements पर Iteration करने के लिए भी किया जा सकता है। यह Explicit Iterator का उपयोग किए बिना Collection के Elements पर कार्य करने की सुविधा प्रदान करता है।
Java Program: Display ArrayList Elements (ArrayList के Elements प्रदर्शित करना)
import java.util.ArrayList;public class ForEachCollection {public static void main(String[] args) {ArrayList<String> fruits = new ArrayList<>();fruits.add("Apple");fruits.add("Mango");fruits.add("Banana");fruits.add("Orange");for (String fruit : fruits) {System.out.println(fruit);}}}
Output:
AppleMangoBananaOrange
Explanation (व्याख्या):
In this program, an ArrayList named fruits stores four fruit names. The enhanced for loop accesses each element of the collection and displays it.
इस प्रोग्राम में fruits नाम का एक ArrayList चार फलों के नाम Store करता है। Enhanced for Loop Collection के प्रत्येक Element को Access करता है और उसे प्रदर्शित करता है।
4. Calculating Sum Using Enhanced for Loop (Enhanced for Loop से योग ज्ञात करना)
The enhanced for loop can also be used to perform calculations on array elements, such as calculating their sum or finding the average.
Enhanced for Loop का उपयोग Array के Elements पर गणना करने के लिए भी किया जा सकता है, जैसे उनका योग या औसत ज्ञात करना।
Java Program: Calculate the Sum of Array Elements (Array Elements का योग ज्ञात करना)
public class ForEachSum {public static void main(String[] args) {int numbers[] = {10, 20, 30, 40, 50};int sum = 0;for (int num : numbers) {sum = sum + num;}System.out.println("Sum = " + sum);}}
Output:
Sum = 150Explanation (व्याख्या):
In this program, the enhanced for loop accesses each element of the numbers array. Each element is added to the sum variable. After all iterations are completed, the total sum is displayed.
इस प्रोग्राम में Enhanced for Loop numbers Array के प्रत्येक Element को Access करता है। प्रत्येक Element को sum वेरिएबल में जोड़ा जाता है। सभी Iterations पूरे होने के बाद कुल योग प्रदर्शित किया जाता है।
Difference Between Traditional for Loop and Enhanced for Loop (Traditional for Loop और Enhanced for Loop में अंतर)
| Traditional for Loop | Enhanced for Loop |
|---|---|
| It uses an index or loop control variable to access array elements. | It directly accesses elements one by one. |
| Initialization, condition, and update are generally specified in the loop header. | It uses a variable and an array or collection separated by a colon. |
| It allows access to elements using their indexes. | It does not directly provide the index of the current element. |
| It is useful when elements need to be accessed, modified by index, or traversed in a specific order. | It is useful when all elements need to be read or processed sequentially. |
| It can execute in forward or reverse order with suitable conditions. | It traverses elements in the iteration order provided by the array or collection. |
Traditional for Loop | Enhanced for Loop |
|---|---|
| इसमें Array Elements को Access करने के लिए Index या Loop Control Variable का उपयोग किया जाता है। | इसमें Elements को सीधे एक-एक करके Access किया जाता है। |
| Initialization, Condition और Update सामान्यतः Loop Header में दिए जाते हैं। | इसमें Variable और Array या Collection को Colon से अलग करके लिखा जाता है। |
| इसमें Index की सहायता से Elements को Access किया जा सकता है। | इसमें वर्तमान Element का Index सीधे प्राप्त नहीं होता। |
| इसका उपयोग Index के आधार पर Elements को Access या Modify करने तथा विशेष क्रम में Traversal करने के लिए किया जाता है। | इसका उपयोग सभी Elements को क्रम से पढ़ने या Process करने के लिए किया जाता है। |
| उचित Conditions के साथ इसे Forward या Reverse Order में चलाया जा सकता है। | यह Array या Collection द्वारा निर्धारित Iteration Order में Elements को Access करता है। |
Limitations of Enhanced for Loop (Enhanced for Loop की सीमाएँ)
Although the enhanced for loop is simple and convenient, it has some limitations:
It does not directly provide the index of the current element.
It is not suitable when elements need to be accessed in reverse order using an index.
It does not allow direct replacement of array elements by assigning a new value to the loop variable.
Structural modification of a collection during iteration may cause
ConcurrentModificationExceptionfor fail-fast collections.
Enhanced for Loop सरल और सुविधाजनक है, लेकिन इसकी कुछ सीमाएँ भी हैं:
यह वर्तमान Element का Index सीधे प्रदान नहीं करता।
Index की सहायता से Elements को Reverse Order में Access करने के लिए यह उपयुक्त नहीं है।
Loop Variable को नया मान देने से Array का मूल Element सीधे Modify नहीं होता।
Iteration के दौरान किसी Collection की संरचना में बदलाव करने पर Fail-fast Collections में
ConcurrentModificationExceptionउत्पन्न हो सकता है।
Note: In an enhanced for loop, the loop variable receives the current element's value. Reassigning that variable does not replace the corresponding array element. To modify array elements, use a traditional for loop with an index.
नोट: Enhanced for Loop में Loop Variable को वर्तमान Element का मान प्राप्त होता है। उस Variable को नया मान देने से Array का संबंधित Element Replace नहीं होता। Array Elements को Modify करने के लिए Index के साथ Traditional for Loop का उपयोग करें।
The Enhanced for loop, also known as the for-each loop, provides a simple and readable way to traverse arrays and collections in Java. It eliminates the need to manage indexes manually and is especially useful when every element needs to be processed. However, when index-based access or modification is required, the traditional for loop is more suitable.
Enhanced for Loop, जिसे For-Each Loop भी कहा जाता है, Java में Arrays और Collections के Elements को Access करने का सरल और पठनीय तरीका प्रदान करता है। इसमें Index को स्वयं नियंत्रित करने की आवश्यकता नहीं होती और यह विशेष रूप से तब उपयोगी होता है, जब प्रत्येक Element पर कार्य करना हो। हालाँकि, जब Index के आधार पर Elements को Access या Modify करना आवश्यक हो, तब Traditional for Loop अधिक उपयुक्त होता है।
Comments
Post a Comment