How to get rows and columns if arrays is only 1D using for loops

arrays, for-loop, java

Solution

 int rows = 3;
 int cols = 4;
 int[] array = new int[rows*cols];
 int[] currentRow = new int[cols];
 for (int i = 0; i < rows; ++i) {
     for (int j = 0; j < cols; ++j) {
         currentRow[j] = array[i*cols + j];
     }
 }

Problem

I was used to `Matlab`'s feature where you can make a matrix and get `A[i][j]` and things like that. Now I am using Java and we can only use one dimensional array. I am suppose to modify the entries (i:for rows and j:for columns) using a nested for loop but I am not sure how to access them if they are stored in an 1D array. Can someone please help me out? How difficult is it?

Original source

Related problems