2-D arrays
Java for AP CS A Lesson 12 1:41 English narration · English + 中文 subtitles burned in
Chapters
Transcript
A 2-D array is a grid — really, an array whose items are themselves arrays, one per row.
二维数组是一张网格—— 说到底,是一个"元素本身也是数组"的数组,一行一个。
To reach a cell you give two indexes, and the order is fixed: row first, then column.
要取到某一格,你给两个索引,而顺序是定死的: 先行,后列。
So the highlighted cell here is row one, column two, and its value is six.
所以这里高亮的这一格是"第 1 行、第 2 列",它的值是 6。
Swapping those two numbers is the classic mistake, and it is the one thing to get right in this whole lesson.
把这两个数字调过来,是最经典的错误, 而这整节课要弄对的就是这一件事。
Because it is an array of arrays, there are two lengths and they mean different things.
因为它是"数组的数组",所以有两个 length,而且含义不同。
grid dot length counts the ROWS — there are two.
grid.length 数的是"行"——这里是 2。
To count the columns you have to go into a row: grid zero dot length counts its columns, and there are three.
要数列,你必须先进到某一行里去: grid .length 数的是那一行的列数,这里是 3。
Outer length is rows; you have to step into a row to ask about columns.
外层的 length 是行数; 要问列数,你得先迈进一行。
To visit every cell you nest two loops: the outer one over the rows and the inner one over the columns.
要走遍每一格,你嵌套两层循环: 外层走行,内层走列。
Watch where it goes — all the way along the first row, then down and all the way along the second.
看它是怎么走的—— 先把第一行从头走到尾,然后换到下一行,再从头走到尾。
That is called row-major order, and it walks the grid the way you read a page.
这叫"行主序", 它走过网格的方式,就跟你读一页书一样。
Four things to take with you.
带走四点。
One: a 2-D array is an array of rows.
第一:二维数组是"行的数组"。
Two: grid of r of c is row first, then column.
第二:grid 是先行、后列。
Three: grid dot length is rows and grid zero dot length is columns.
第三:grid.length 是行数,grid .length 是列数。
Four: row-major means outer rows, inner columns.
第四:行主序就是外层走行、内层走列。
Now work with a grid in the tasks below.
现在去下面的题里练一练网格。