java 行转列 列转行
时间: 2023-08-08 20:10:36 浏览: 56
Java中实现行转列和列转行可以使用二维数组进行操作。
行转列:
```java
public static int[][] transpose(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int[][] result = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[j][i] = matrix[i][j];
}
}
return result;
}
```
列转行:
```java
public static int[][] convert(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int[][] result = new int[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
result[i][j] = matrix[j][i];
}
}
return result;
}
```
你可以将需要转换的二维数组传入这两个方法中,分别获得行转列和列转行的结果。