英文:
How to write a method for a transposed matrix?
问题
我正在尝试创建一个名为MatrixApplication
的应用程序类,在其中用户首先输入矩阵的行数和列数。
这将用于创建一个数组对象。
然后按行和列调用Matrix
的元素。当所有元素都已读取时,它们将被分配给矩阵对象。
接下来,数组将被转置,最后显示转置后的数组。
如何将元素分配给Matrix对象?
如何显示转置后的数组?
package domain;
public class Matrix {
private int[][] numbers;
public Matrix(int rows, int columns) {
setNumbers(numbers);
if (rows < 1)
rows = 1;
else
rows = rows;
if (columns < 1)
columns = 1;
else
columns = columns;
numbers = new int[rows][columns];
}
public final void setNumbers(int[][] numbers) {
this.numbers = numbers;
}
public int[][] getNumbers() {
return numbers;
}
public int[][] transpose() {
int[][] transpose = new int[numbers[0].length][numbers.length];
for (int i = 0; i < numbers.length; ++i) {
for (int j = 0; j < numbers[0].length; ++j) {
transpose[j][i] = numbers[i][j];
}
}
return transpose;
}
}
package ui;
import java.util.Scanner;
import domain.Matrix;
public class MatrixApplication {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("输入矩阵的行数:");
int rows = input.nextInt();
System.out.print("输入矩阵的列数:");
int columns = input.nextInt();
Matrix matrix = new Matrix(rows, columns);
final int[][] numbers = new int[rows][columns];
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
System.out.printf("输入第 %d 行第 %d 列的元素:", i + 1, j + 1);
numbers[i][j] = input.nextInt();
}
}
int[][] transposedMatrix = matrix.transpose();
System.out.println("转置后的矩阵:");
for (int i = 0; i < transposedMatrix.length; ++i) {
for (int j = 0; j < transposedMatrix[0].length; ++j) {
System.out.print(transposedMatrix[i][j] + " ");
}
System.out.println();
}
}
}
如果您想要将转置后的矩阵以特定形式显示,例如将4x2数组转置为2x4数组,请参考以下示例图像:示例图像。这需要更改转置矩阵的显示方式,以符合所需的格式。
英文:
I am trying to create an application class MatrixApplication
, in which the user first enters the number of rows and columns of the matrix.
This will be used to create an array object.
Then the elements of the Matrix
are called up row by row and column by column. When all elements have been read in, they are assigned to the matrix object.
Next, the array is transposed and finally the transposed array is displayed.
How do I assign the elements to the Matrix object?
How do I display a transposed array?
package domain;
public class Matrix {
private int[][] numbers;
public Matrix(int rows, int columns) {
setNumbers(numbers);
if (rows < 1)
rows = 1;
else
rows = rows;
if (columns < 1)
columns = 1;
else
columns = columns;
numbers = new int[rows][columns];
}
public final void setNumbers(int[][] numbers) {
this.numbers = numbers;
}
public int[][] getNumbers() {
return numbers;
}
public int[][] transpose() {
int[][] transpose = new int[numbers[0].length][numbers.length];
for (int i = 0; i < numbers.length; ++i) {
for (int j = 0; j < numbers[0].length; ++j) {
transpose[j][i] = numbers[i][j];
}
}
return transpose;
}
}
package ui;
import java.util.Scanner;
import domain.Matrix;
public class MatrixApplication {
public static void main (String[]args)
{
Scanner input = new Scanner (System.in);
System.out.print("Enter the number of rows of the matrix:");
int rows = input.nextInt();
System.out.print("nter the number of columns of the matrix:");
int colums = input.nextInt();
Matrix matrix = new Matrix(rows, colums);
final int[][] numbers = new int[rows][colums];
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < colums; ++j) {
System.out.printf("Enter the element of row %d and column %d: ", i + 1, j + 1);
numbers[i][j] = input.nextInt();
}
}
}
System.out.printf("The transposed matrix: %d",matrix.transpose());
}
}
And if I want this form of transposed matrix:
example of a 4x2 array to a 2x4 array
答案1
得分: 1
只需将数字读入一个二维数组中,并调用 matrix.setNumbers
。
final int[][] numbers = new int[rows][columns];
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
System.out.printf("Enter the element of row %d and column %d: ", i + 1, j + 1);
numbers[i][j] = input.nextInt();
}
}
matrix.setNumbers(numbers);
System.out.printf("The transposed matrix: %s", Arrays.deepToString(matrix.transpose()));
英文:
Simply read the numbers into a two-dimensional array and call matrix.setNumbers
.
final int[][] numbers = new int[rows][colums];
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < colums; ++j) {
System.out.printf("Enter the element of row %d and column %d: ", i + 1, j + 1);
numbers[i][j] = input.nextInt();
}
}
matrix.setNumbers(numbers);
System.out.printf("The transposed matrix: %s", Arrays.deepToString(matrix.transpose()));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论