Row-major vs column-major 2D traversal二维数组的行优先与列优先遍历
Run one Java statement or condition check at a time. Watch g, r, c, rowCode, and colCode change.每次执行一条 Java 语句或检查一次条件,观察 g、r、c、rowCode、colCode 的变化。
Code代码
- int[][] g = {{1, 2, 3}, {4, 5, 6}};
- int rowCode = 0;
- for (int r = 0; r < g.length; r++) {
- for (int c = 0; c < g[0].length; c++) {
- rowCode = rowCode * 10 + g[r][c];
- }
- }
- int colCode = 0;
- for (int c = 0; c < g[0].length; c++) {
- for (int r = 0; r < g.length; r++) {
- colCode = colCode * 10 + g[r][c];
- }
- }
State状态
| Variable变量 | Value值 |
|---|---|
| g | [[1, 2, 3], [4, 5, 6]] |
| r | — |
| c | — |
| rowCode | — |
| colCode | — |
| Condition条件 | — |
Use ← and → to step. The final condition is false, so the loop stops.使用 ← 和 → 逐步运行。最后条件为假,循环结束。