在Java中,可以通過以下方法將二維數(shù)組進(jìn)行轉(zhuǎn)置:
public static int[][] transpose(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int[][] transposedMatrix = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transposedMatrix[j][i] = matrix[i][j];
}
}
return transposedMatrix;
}
調(diào)用此方法,可以將一個二維數(shù)組轉(zhuǎn)置,并返回轉(zhuǎn)置后的二維數(shù)組。