Repository navigation
Expand file tree
/
Copy pathEnhancedForLoop.java
More file actions
59 lines (51 loc) · 1.91 KB
/
Copy pathEnhancedForLoop.java
File metadata and controls
59 lines (51 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package javakb.ch05_arrays;
/**
* EnhancedForLoop demonstrates the 'for-each' loop in Java.
*
* Syntax:
* for (ElementType variable : arrayOrCollection) {
* // body
* }
*
* Advantages:
* - Cleaner, simpler syntax (no manual index counter or length check required).
* - Prevents off-by-one errors and ArrayIndexOutOfBoundsException.
*
* Limitations:
* - Read-only traversal: modifying the iteration variable does NOT change array elements.
* - No access to element index.
* - Cannot iterate backwards or skip elements.
*/
public class EnhancedForLoop {
public static void main(String[] args) {
String[] languages = {"Java", "Python", "C++", "JavaScript", "Go"};
int[] numbers = {10, 20, 30, 40, 50};
System.out.println("--- 1. Simple For-Each Traversal over Strings ---");
for (String lang : languages) {
System.out.println("Language: " + lang);
}
System.out.println("\n--- 2. Summing Array Elements with For-Each ---");
int totalSum = 0;
for (int num : numbers) {
totalSum += num;
}
System.out.println("Total Sum: " + totalSum);
System.out.println("\n--- 3. Limitation Demonstration: Read-Only Nature ---");
System.out.print("Before loop: ");
printArray(numbers);
// Attempting to modify element via for-each loop
for (int num : numbers) {
num = num * 2; // Changes local copy 'num', NOT the array!
}
System.out.print("After for-each modification attempt: ");
printArray(numbers);
System.out.println("Notice: Array elements remain unchanged because 'num' is a local copy!");
}
private static void printArray(int[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + (i < arr.length - 1 ? ", " : ""));
}
System.out.println("]");
}
}