如何交换二维数组的行和列?
How to swap rows and columns of a 2d array?
我正在尝试为 'transpose' 二维整数数组编写一个方法,其中交换原始矩阵的行和列。
但是,我不知道如何实现这一点。
这个方法怎么写出来?
public class Matrix {
private int[][] numbers;
public Matrix(int rows, int colums) {
if (rows < 1)
rows = 1;
else
rows = rows;
if (colums < 1)
colums = 1;
else
colums = colums;
numbers = new int[rows][colums];
}
public final void setNumbers(int[][] numbers) {
this.numbers = numbers;
}
public int[][] getNumbers() {
return numbers;
}
public int[][] transpose() {
int[][] transpose = getNumbers();
return numbers;
}
}
您可以遍历行和列并将每个元素 [i,j] 分配给转置后的 [j,i]:
/**
* Transposses a matrix.
* Assumption: mat is a non-empty matrix. i.e.:
* 1. mat != null
* 2. mat.length > 0
* 3. For every i, mat[i].length are equal and mat[i].length > 0
*/
public static int[][] transpose(int[][] mat) {
int[][] result = new int[mat[0].length][mat.length];
for (int i = 0; i < mat.length; ++i) {
for (int j = 0; j < mat[0].length; ++j) {
result[j][i] = mat[i][j];
}
}
return result;
}
矩阵的转置
int m = 4;
int n = 5;
// original matrix
int[][] arr1 = {
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25},
{26, 27, 28, 29, 30}};
// transposed matrix
int[][] arr2 = new int[n][m];
// swap rows and columns
IntStream.range(0, n).forEach(i ->
IntStream.range(0, m).forEach(j ->
arr2[i][j] = arr1[j][i]));
// output to the markdown table
String matrices = Stream.of(arr1, arr2)
.map(arr -> Arrays.stream(arr).map(Arrays::toString)
.collect(Collectors.joining("<br>")))
.collect(Collectors.joining("</pre> | <pre>"));
System.out.println("| original matrix | transposed matrix |");
System.out.println("|---|---|");
System.out.println("| <pre>" + matrices + "</pre> |");
original matrix
transposed matrix
[11, 12, 13, 14, 15]
[16, 17, 18, 19, 20]
[21, 22, 23, 24, 25]
[26, 27, 28, 29, 30]
[11, 16, 21, 26]
[12, 17, 22, 27]
[13, 18, 23, 28]
[14, 19, 24, 29]
[15, 20, 25, 30]
另请参阅:
我正在尝试为 'transpose' 二维整数数组编写一个方法,其中交换原始矩阵的行和列。
但是,我不知道如何实现这一点。 这个方法怎么写出来?
public class Matrix {
private int[][] numbers;
public Matrix(int rows, int colums) {
if (rows < 1)
rows = 1;
else
rows = rows;
if (colums < 1)
colums = 1;
else
colums = colums;
numbers = new int[rows][colums];
}
public final void setNumbers(int[][] numbers) {
this.numbers = numbers;
}
public int[][] getNumbers() {
return numbers;
}
public int[][] transpose() {
int[][] transpose = getNumbers();
return numbers;
}
}
您可以遍历行和列并将每个元素 [i,j] 分配给转置后的 [j,i]:
/**
* Transposses a matrix.
* Assumption: mat is a non-empty matrix. i.e.:
* 1. mat != null
* 2. mat.length > 0
* 3. For every i, mat[i].length are equal and mat[i].length > 0
*/
public static int[][] transpose(int[][] mat) {
int[][] result = new int[mat[0].length][mat.length];
for (int i = 0; i < mat.length; ++i) {
for (int j = 0; j < mat[0].length; ++j) {
result[j][i] = mat[i][j];
}
}
return result;
}
矩阵的转置
int m = 4;
int n = 5;
// original matrix
int[][] arr1 = {
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25},
{26, 27, 28, 29, 30}};
// transposed matrix
int[][] arr2 = new int[n][m];
// swap rows and columns
IntStream.range(0, n).forEach(i ->
IntStream.range(0, m).forEach(j ->
arr2[i][j] = arr1[j][i]));
// output to the markdown table
String matrices = Stream.of(arr1, arr2)
.map(arr -> Arrays.stream(arr).map(Arrays::toString)
.collect(Collectors.joining("<br>")))
.collect(Collectors.joining("</pre> | <pre>"));
System.out.println("| original matrix | transposed matrix |");
System.out.println("|---|---|");
System.out.println("| <pre>" + matrices + "</pre> |");
original matrix | transposed matrix |
---|---|
[11, 12, 13, 14, 15] |
[11, 16, 21, 26] |
另请参阅: