converting the index of 1D array into 2D array

妖精的绣舞 提交于 2021-02-04 21:37:18

问题


How can I convert the index of 1D array into 2D array? I know how to convert a 2D array into 1D (i*the size of row+j). I want the opposite of that.


回答1:


What you need to know is: How many columns should the 2D array have: Lets say you have an array of 20 columns and 10 rows (array[20,10]):

int index  = 47;
int numberOfColumns = 20;
int column = index % numberOfColumns;
int row    = index / numberOfColumns;

// column == 7
// row    == 2



回答2:


You can just do the opposite. if n is the length of the row and x is index in 1D. You can index like

array[x/n][x%n]


来源:https://stackoverflow.com/questions/41433202/converting-the-index-of-1d-array-into-2d-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!