Convert a 2D array index into a 1D index

后端 未结 5 1321
说谎
说谎 2020-12-15 20:49

I have two arrays for a chess variant I am coding in java...I have a console version so far which represents the board as a 1D array (size is 32) but I am working on making

5条回答
  •  醉梦人生
    2020-12-15 21:02

    Every row in your 2D array is placed end to end in your 1D array. i gives which row you are in, and j gives the column (how far into that row). so if you are in the ith row, you need to place i complete rows end to end, then append j more onto that to get your single array index.

    So it will be something like
    singleDimIndex = array[0].length * i + j

提交回复
热议问题