← Back to Study Guide← 返回学习指南

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代码

  1. int[][] g = {{1, 2, 3}, {4, 5, 6}};
  2. int rowCode = 0;
  3. for (int r = 0; r < g.length; r++) {
  4. for (int c = 0; c < g[0].length; c++) {
  5. rowCode = rowCode * 10 + g[r][c];
  6. }
  7. }
  8. int colCode = 0;
  9. for (int c = 0; c < g[0].length; c++) {
  10. for (int r = 0; r < g.length; r++) {
  11. colCode = colCode * 10 + g[r][c];
  12. }
  13. }

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.使用 ← 和 → 逐步运行。最后条件为假,循环结束。