AI文库-您身边的智能助手,如没有想要的内容请与本站万能的AI助手联系。

用Java写出矩阵转置

来自 湖北省武汉市 的网友 时间: 热度:°C 加入收藏 我要投稿 点赞()
```java
public class MatrixTranspose {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

int[][] transposedMatrix = transpose(matrix);

for (int i = 0; i < transposedMatrix.length; i++) {
for (int j = 0; j < transposedMatrix[i].length; j++) {
System.out.print(transposedMatrix[i][j] + " ");
}
System.out.println();
}
}

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;
}
}
```
221381
领取福利

微信扫码领取福利

微信扫码分享