How to put one dimensional array in 2d array java

First, there are bugs in your original code: array should have 16 elements, not 15. Also, the end conditions for your loops are incorrect. Then add either

array2[i][j] = array[i*4 + j];

or, depending on whether you want row-major or column-major order:

array2[i][j] = array[j*4 + i];

Or more generally:

array2[i][j] = array[(i * width) + j];

Leave a Comment