-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo2dArray.java
More file actions
69 lines (46 loc) · 1.48 KB
/
Demo2dArray.java
File metadata and controls
69 lines (46 loc) · 1.48 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
60
61
62
63
64
65
66
67
68
69
public class Demo2dArray
{
public static void main(String[] args)
{
// this creates a 2-by-5 table (2-D array)
String[][] table= new String[2][5];
//table.length = length of each row (how many columns)
// this nested for-loop sets every "cell" to 0
for(int r=0; r<table.length; r++) // outer loop = rows
{
for(int c=0; c<table[r].length; c++) // inner loop = columns
{
table[r][c]="Moo";
}
}
// this nested for-loop DISPLAYS the table
for(int r=0; r<table.length; r++) // outer loop = rows
{
for(int c=0; c<table[r].length; c++) // inner loop = columns
{
System.out.print(table[r][c]);
}
System.out.println(); // after each row,
// move to the next line
}
int row = 0, column = 0;
System.out.println("\nWhere would you like to place a 1?");
System.out.print("Row: ");
row=SavitchIn.readLineInt();
System.out.print("Column: ");
column=SavitchIn.readLineInt();
table[row][column]="1";
System.out.println("\nHere is the new table:");
// this nested for-loop DISPLAYS the table
for(int r=0; r<table.length; r++) // outer loop = rows
{
for(int c=0; c<table[r].length; c++) // inner loop = columns
{
System.out.print(table[r][c]);
}
System.out.println(); // after each row,
// move to the next line
}
System.out.println("\n\n");
}
}