-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwodarray.java
More file actions
40 lines (32 loc) · 1.22 KB
/
Copy pathtwodarray.java
File metadata and controls
40 lines (32 loc) · 1.22 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
public class twodarray {
public static void main(String[] args){
// 2D array = An array where each element is an array
// Useful for storing a matrix of data
//-----------------------
// EXAMPLE 1
//-----------------------
String[][] groceries = {{"apple", "orange", "banana"},
{"potato", "onion", "carrot"},
{"chicken", "pork", "beef", "fish"}};
groceries[2][1] = "eggs";
for(String[] foods : groceries){
for(String food : foods){
System.out.print(food + " ");
}
System.out.println();
}
//-----------------------
// EXAMPLE 2
//-----------------------
char[][] telephone = {{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}};
for(char[] row : telephone){
for(char number : row){
System.out.print(number + " ");
}
System.out.println();
}
}
}