I have a row-based multidimensional array:
/** [row][column]. */
public int[][] tiles;
I would like to transform this array to column-based
Use this function (replace String by int if necessary). It takes the matrix as string array and returns a new matrix which is the transpose. It checks for the edge case of an empty array, too. No prints.
private String[][] transposeTable(String[][] table) {
// Transpose of empty table is empty table
if (table.length < 1) {
return table;
}
// Table isn't empty
int nRows = table.length;
int nCols = table[0].length;
String[][] transpose = new String[nCols][nRows];
// Do the transpose
for (int i = 0; i < nRows; i++) {
for (int j = 0; j < nCols; j++) {
transpose[j][i] = table[i][j];
}
}
return transpose;
}