← Back to Core Java Course | Chapter 4: Control Flow | Lesson 8 of 9

Java का For-Each Loop

For-each loop किसी array या collection के हर element पर sequence में iterate करता है, जब current index की ज़रूरत न हो तो एक traditional indexed for loop से simpler syntax उपयोग करते हुए।
Syntax
java
for (dataType item : collection) {
    // use item
}

Basic For-Each Syntax

For-each loop, enhanced for loop भी कहलाता है, syntax for (type variable : source) उपयोग करके किसी array या collection के हर element पर sequence में iterate करता है, किसी counter variable या explicit length check की ज़रूरत के बिना।

उदाहरण: Basic For-Each Syntax

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare numbers and set it to {1, 2, 3}
		int[] numbers = {1, 2, 3};
		for (int n : numbers) {
			// Print a line to the console
			System.out.println(n);
		}
	}
}

For-Each over Arrays

For-each किसी भी type की arrays के साथ काम करता है, int, double, और char जैसे primitives सहित, array की natural order में पहले index से आखिरी तक बिना किसी manual indexing के हर element पढ़ते हुए।

उदाहरण: For-Each over Arrays

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare prices and set it to {9.99, 4.50, 12.25}
		double[] prices = {9.99, 4.50, 12.25};
		for (double price : prices) {
			// Print a line to the console
			System.out.println(price);
		}
	}
}

For-Each over Collections

For-each किसी भी Java collection पर भी काम करता है जो Iterable implement करती है, जैसे ArrayList, HashSet, या एक Map का entrySet, इसे arrays और collection types दोनों में उसी syntax के साथ loop करने का एक consistent तरीका बनाते हुए।

उदाहरण: For-Each over Collections

java
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new ArrayList object and store it in names
		ArrayList<String> names = new ArrayList<>();
		names.add("Alice");
		names.add("Bob");
		for (String name : names) {
			// Print a line to the console
			System.out.println(name);
		}
	}
}

For-Each with 2D Arrays

For-each से एक multi-dimensional array iterate करने के लिए दो loops nest करने चाहिए: outer loop हर row को एक छोटे array के रूप में retrieve करता है, और inner loop फिर उस row के अंदर individual values पर iterate करता है।

उदाहरण: For-Each with 2D Arrays

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		int[][] grid = {{1, 2}, {3, 4}};
		for (int[] row : grid) {
			for (int value : row) {
				// Print to the console without a newline
				System.out.print(value + " ");
			}
		}
	}
}

Limitations of For-Each

For-each current index access या modify नहीं कर सकता, iteration के दौरान collection से elements सुरक्षित रूप से नहीं हटा सकता, और loop variable को खुद reassign करने का underlying array या collection पर कोई effect नहीं।

उदाहरण: Limitations of For-Each

java
public class Main {
	public static void main(String[] args) {
		int[] numbers = {1, 2, 3};
		for (int n : numbers) {
			n = 99; // reassigning n has no effect on numbers[]
		}
		System.out.println(numbers[0]);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. For-each उपयोग करते समय collection modify करने की कोशिश करना, जो ConcurrentModificationException throw करता है।
  2. यह उम्मीद करना कि loop variable को assign करना array element बदल देगा, जब यह primitives के लिए सिर्फ एक copy बदलता है।
  3. Index चाहिए होने पर for-each उपयोग करना, जो यह नहीं देता।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.