CALCULATION METHOD OF INDEX WHEN HANDLING TWO-DIMENSIONAL DATA IN ONE-

Asked 1 years ago, Updated 1 years ago, 87 views

I was told that in order to treat a one-dimensional array as a two-dimensional array, it is a width × row position + column position, but I am not sure.
Here's the code.

var SIDE_CELLS=5;// Width
var COLUMN_CELLS=3;// Vertical Width
varci = new Array (SIDE_CELLS*COLUMN_CELLS);
var row=';
for(varz=0;z<ci.length;++z){
  ci[z] = z+1;
  row=row+(ci[z]<10?':')+ci[z];
}
console.log(row);
for(vary=0;y<COLUMN_CELLS;++y){
  row=';
  for (varx=0;x<SIDE_CELLS;++x){
    varz=y*SIDE_CELLS+x;
    row=row+(ci[z]<10?':')+ci[z];
  }
  console.log(row);
}

The code varz=y*SIDE_CELLS+x; appears to be a two-dimensional array. I'm weak-headed, so I don't know.
Even if I try to think about it as shown in the picture, I can't grasp the image at all.
I've been thinking about it for two days. Please help me.

javascript array

2022-09-30 11:27

1 Answers

varz=y*SIDE_CELLS+x; is the index (addressed).

3x5 array

[
  [1,2,3,4,5], // row: 0, the first column of row 0 contains 2 (0 start)
  [6, 7, 8, 9, 10], // row: 1, row 2, column 8
  [11, 12, 13, 14, 15] // row:2
]

If is a one-dimensional array,

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] It's just a row.

The index is
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
Contents: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15]

by the number of columns 5 {0,1,2,3,4}, {5,6,7,8,9}, {10,11,12,13,14}

You can see that each has a leading number + {0,1,2,3,4}.
The leading number of each connection is
Row subscripts x columns (i.e., multiple of 5 (number of columns) starting from 0×5, 1×5, 2×5:0) and
You can see that {0,1,2,3,4} is a column subscript.
So,
Secondary Array [line subscripts][Column subscripts] is the first array [column subscripts x columns + column subscripts]
(that is, the secondary array [1][2] is the same (pointing to 8) as the primary array [1×5+2])
You can see that there is a connection.


2022-09-30 11:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.