如果数组仅使用for循环1D,如何获取行和列

Ama*_*ama 2 java arrays for-loop

我曾经习惯于Matlab制作矩阵并获得A[i][j]类似的东西.现在我使用的是Java,我们只能使用一维数组.我想使用嵌套的for循环来修改条目(i:for rows和j:for columns)但是如果它们存储在一维数组中我不知道如何访问它们.有人可以帮帮我吗?这有多难?

Rob*_*b G 6

 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];
     }
 }
Run Code Online (Sandbox Code Playgroud)


gki*_*iko 5

private int getElem(int[] arr, int i, int j){
  return arr[i*colNum+j];
}
Run Code Online (Sandbox Code Playgroud)