2-D arrays: working with a grid · 二维数组:操作网格
A grid of values
- A normal array is a single row of values.
- A 2-D array is a grid: it has rows and columns, like a table or a spreadsheet.
- We use it for game boards, images, tables of marks, and more.
一个数值的网格
- 普通的数组是一行数值。
- 二维数组(2-D array)是一个网格(grid):它有行(row)和列(column),就像一张表格或电子表格。
- 我们用它来表示棋盘、图像、成绩表等等。
Making a 2-D array
int[][] g = new int[3][4];makes a grid with 3 rows and 4 columns.- Every cell starts at
0(forint). - You can also fill it directly with values inside
{...}.
创建一个二维数组
int[][] g = new int[3][4];创建一个有 3 行和 4 列的网格。- 每个格子(cell)一开始都是
0(对于int)。 - 你也可以在
{...}里面直接用值把它填好。
public class Main {
public static void main(String[] args) {
int[][] g = new int[3][4]; // 3 rows, 4 columns, all 0
int[][] table = {
{1, 2, 3},
{4, 5, 6}
}; // 2 rows, 3 columns
System.out.println(table[0][2]); // 3
System.out.println(table[1][0]); // 4
}
}
Reading and writing one cell
- A cell needs two indexes:
g[row][col]. Row first, then column. - Both indexes start at 0.
g[1][2] = 9;stores9in row1, column2.
读取和写入一个格子
- 一个格子需要两个下标:
g[row][col]。先行,后列。 - 两个下标都从 0 开始。
g[1][2] = 9;把9存进第1行、第2列。
public class Main {
public static void main(String[] args) {
int[][] g = new int[2][2];
g[0][0] = 5;
g[0][1] = 7;
g[1][0] = 9;
g[1][1] = 11;
System.out.println(g[1][0]); // 9
g[1][0] = g[1][0] + 1;
System.out.println(g[1][0]); // 10
}
}
How big is the grid?
g.length— the number of rows.g[0].length— the number of columns in row0.- In AP CSA every row has the same length, so
g[0].lengthis the column count for the whole grid.
网格有多大?
g.length—— 行的数量。g[0].length—— 第0行里列的数量。- 在 AP CSA 中,每一行的长度都相同,所以
g[0].length就是整个网格的列数。
public class Main {
public static void main(String[] args) {
int[][] g = {
{1, 2, 3},
{4, 5, 6}
};
System.out.println(g.length); // 2 rows
System.out.println(g[0].length); // 3 columns
}
}
Row-major traversal
- To visit every cell, use a loop inside a loop.
- The outer loop picks the row
r. The inner loop picks the columnc. - This visits row
0fully, then row1, and so on. This order is called row-major.
行优先遍历
- 要访问每一个格子,使用一个循环嵌套在另一个循环里面。
- 外层循环选择行
r。内层循环选择列c。 - 这样会先完整地走完第
0行,再走第1行,以此类推。这种顺序叫作行优先(row-major)。
public class Main {
public static void main(String[] args) {
int[][] g = {
{1, 2, 3},
{4, 5, 6}
};
int total = 0;
for (int r = 0; r < g.length; r++) {
for (int c = 0; c < g[0].length; c++) {
total = total + g[r][c];
}
}
System.out.println(total); // 21
}
}
Common mistakes
- Index a grid
grid[row][col]. grid.lengthis the number of rows;grid[0].lengthis the columns.
常见错误
- 网格索引
grid[row][col]。 grid.length是行数;grid[0].length是列数。
Now you try
- Each task pre-fills the class skeleton — write your code inside main, or complete the method shown.
- Press Run to compile and run, then Check answer.
- Your code compiles and runs on the server, so even the first run is fast.
现在轮到你
- 每个任务都已经填好了类的骨架 —— 把你的代码写在 main 里面,或者补全给出的方法。
- 按运行来编译并运行,然后按检查答案。
- 你的代码在服务器上编译并运行,所以第一次运行也很快。
A 2-D array is a grid · 二维数组就是网格
A 2-D array is rows × columns; reach a cell with a[row][col]. · 二维数组是行 × 列;用 a[row][col] 取一个格子。
Complete sumGrid(int[][] g) so it returns the total · 总 of every cell in the grid. Use a nested loop with g.length rows and g[0].length columns. · 完成 sumGrid(int[][] g),让它返回网格里所有格子的总和。使用嵌套循环,行数是 g.length,列数是 g[0].length。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete rowSum(int[][] g, int r) so it returns the total of the cells in row · 行 r only. Loop over the columns of that one row. · 完成 rowSum(int[][] g, int r),让它只返回第 r 行各格子的总和。遍历那一行的各列即可。
Click Run to see the output here. · 点击“运行”查看此处输出。
In main, the grid is given. Print each row · 行 on its own line, with the numbers joined by a single space. For the grid shown, the output is 1 2 3 then 4 5 6. (Build each line in row-major order, then println it.) · 在 main 中,网格已经给好了。把每一行单独打印在一行上,数字之间用一个空格隔开。对于给出的网格,输出是 1 2 3 然后是 4 5 6。(按行优先顺序拼好每一行,再 println 它。)
Click Run to see the output here. · 点击“运行”查看此处输出。