优客文库提供全面的范文格式和文档模板,包括申请书、合同、借据、证明、求职信等,助力您快速撰写专业文档。

用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
领取福利

微信扫码领取福利

微信扫码分享