Java का For-Each Loop
In this page:
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
// 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);
}
}
}
Login to try C/C++/Java/PHP code in the editor
For-Each over Arrays
For-each किसी भी type की arrays के साथ काम करता है, int, double, और char जैसे primitives सहित, array की natural order में पहले index से आखिरी तक बिना किसी manual indexing के हर element पढ़ते हुए।
उदाहरण: For-Each over Arrays
// 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);
}
}
}
Login to try C/C++/Java/PHP code in the editor
For-Each over Collections
For-each किसी भी Java collection पर भी काम करता है जो Iterable implement करती है, जैसे ArrayList, HashSet, या एक Map का entrySet, इसे arrays और collection types दोनों में उसी syntax के साथ loop करने का एक consistent तरीका बनाते हुए।
उदाहरण: For-Each over Collections
// 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);
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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 + " ");
}
}
}
}
Login to try C/C++/Java/PHP code in the editor
Limitations of For-Each
For-each current index access या modify नहीं कर सकता, iteration के दौरान collection से elements सुरक्षित रूप से नहीं हटा सकता, और loop variable को खुद reassign करने का underlying array या collection पर कोई effect नहीं।
उदाहरण: Limitations of For-Each
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]);
}
}
Login to try C/C++/Java/PHP code in the editor
- For-each उपयोग करते समय collection modify करने की कोशिश करना, जो
ConcurrentModificationExceptionthrow करता है। - यह उम्मीद करना कि loop variable को assign करना array element बदल देगा, जब यह primitives के लिए सिर्फ एक copy बदलता है।
- Index चाहिए होने पर for-each उपयोग करना, जो यह नहीं देता।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: