2D Array Creation and Access · 二维数组的创建与访问
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| 2-D array/tuː diː əˈreɪ/ | 二维数组 | èr wéi shù zǔ |
A grid of values
- A 2-D array 二维数组 stores values in a grid of rows and columns.
- Declare with two brackets:
int[][] grid = new int[3][4];(3 rows, 4 columns). - Or an initializer:
int[][] g = {{1, 2}, {3, 4}, {5, 6}};(3 rows of 2). - Think spreadsheet, game board, or pixel image.
值的网格
- 二维数组把值存储在行和列的网格里。
- 用两对方括号声明:
int[][] grid = new int[3][4];(3 行,4 列)。 - 或用初始化列表:
int[][] g = {{1, 2}, {3, 4}, {5, 6}};(3 行、每行 2 个)。 - 想成电子表格、游戏棋盘或像素图像。
Row then column
- Access an element with two indices:
grid[row][col]. grid[0][0]is the top-left;grid[1][2]is row1, column2.- Row comes first, then column — a common thing to get backwards.
- Both indices start at
0, like every Java array.
先行后列
- 用两个下标访问一个元素:
grid[row][col]。 grid[0][0]是左上角;grid[1][2]是第1行、第2列。- 行在前,然后是列——一个常被弄反的事。
- 两个下标都从
0开始,像每个 Java 数组。
Row and column counts
grid.lengthis the number of rows.grid[0].lengthis the number of columns (length of one row).- For a rectangular grid, every row has the same column count.
- Use these two lengths to bound loops over the grid.
行数与列数
grid.length是行的数目。grid[0].length是列的数目(一行的长度)。- 对矩形网格,每行都有相同的列数。
- 用这两个长度给遍历网格的循环设界。
Two bounds to watch
- A valid element is
grid[r][c]with0 <= r < grid.lengthand0 <= c < grid[0].length. - Going out of range on either index throws ArrayIndexOutOfBoundsException.
- Mixing up row and column silently reads the wrong cell (or crashes).
- Double-check
[row][col]order every time.
要留意两个边界
- 有效元素是
grid[r][c],其中0 <= r < grid.length且0 <= c < grid[0].length。 - 在任一下标上越界都会抛出 ArrayIndexOutOfBoundsException。
- 弄混行和列会悄悄读错单元格(或崩溃)。
- 每次都仔细核对
[row][col]顺序。
It's grid[row][col] — row first, then column — and the two lengths differ. grid.length counts rows; grid[0].length counts columns. Swapping the indices reads the wrong cell or throws an exception. When you loop, the outer index goes to grid.length and the inner to grid[0].length.
是 grid[row][col]——先行、后列——而且两个长度不同。grid.length 数行;grid[0].length 数列。对调下标会读错单元格或抛出异常。当你循环时,外层下标走到 grid.length,内层走到 grid[0].length。
A 3×2 grid g = {{1, 2}, {3, 4}, {5, 6}}:
g[0][0]is1;g[2][1]is6(row 2, column 1).g.lengthis3(rows);g[0].lengthis2(columns).g[1][0]is3— row1, column0.
一个 3×2 网格 g = {{1, 2}, {3, 4}, {5, 6}}:
g[0][0]是1;g[2][1]是6(第 2 行,第 1 列)。g.length是3(行);g[0].length是2(列)。g[1][0]是3——第1行,第0列。
A 2-D array is a grid, created with new type[rows][cols] or a nested initializer. Access an element as grid[row][col] (row first). grid.length is the row count; grid[0].length is the column count. Both indices start at 0, and going out of range throws an exception.
二维数组是一个网格,用 new type[rows][cols] 或嵌套初始化列表创建。用 grid[row][col](行在前)访问元素。grid.length 是行数;grid[0].length 是列数。两个下标都从 0 开始,越界会抛出异常。
grid[row][col] indexing · grid[row][col] 索引
g[2][1] = 6; g.length = 3 rows, g[0].length = 2 columns. · g[2][1] = 6;g.length = 3 行,g[0].length = 2 列。
For int[][] g = {{1,2},{3,4},{5,6}}; what is g[2][1]? · 对 int[][] g = {{1,2},{3,4},{5,6}};,g[2][1] 是多少?
Row 2 is {5,6}; column 1 is 6. · 第 2 行是 {5,6};第 1 列是 6。
In grid[row][col], which index comes first? · 在 grid[row][col] 中,哪个下标在前?
Row first, then column. · 先行,后列。
For a 3-row, 4-column grid, what does grid.length equal? · 对一个 3 行 4 列的网格,grid.length 等于多少?
grid.length is the number of rows = 3. · grid.length 是行数 = 3。
For a 3-row, 4-column grid, what does grid[0].length equal? · 对一个 3 行 4 列的网格,grid[0].length 等于多少?
grid[0].length is the number of columns = 4. · grid[0].length 是列数 = 4。
grid.length gives the number of columns. · grid.length 给出列的数目。
grid.length is rows; grid[0].length is columns. · grid.length 是行;grid[0].length 是列。