-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForEach3.java
More file actions
39 lines (38 loc) · 822 Bytes
/
Copy pathForEach3.java
File metadata and controls
39 lines (38 loc) · 822 Bytes
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
// for-each style for on a two-dimensional array
public class ForEach3 {
public static void main(String[] args){
int sum = 0;
int nums[][] = new int[3][5];
//give nums some values
for(int i=0; i<3; i++)
for(int j=0; j<5; j++)
nums[i][j] = (i+1) * (j+1);
//for-each for to display and sum the values
for(int x[] : nums){
for(int y : x){
System.out.println("Value is: " + y);
sum += y;
}
}
System.out.println("Summation: " + sum);
}
}
/*
run:
Value is: 1
Value is: 2
Value is: 3
Value is: 4
Value is: 5
Value is: 2
Value is: 4
Value is: 6
Value is: 8
Value is: 10
Value is: 3
Value is: 6
Value is: 9
Value is: 12
Value is: 15
Summation: 90
*/