问题
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