Java 如何编写转置矩阵的方法?

Java 如何编写转置矩阵的方法?,java,Java,我试图创建一个应用程序类MatrixApplication,用户首先在其中输入矩阵的行数和列数 这将用于创建数组对象。 然后逐行、逐列调用矩阵的元素。读入所有元素后,它们将被指定给矩阵对象 接下来,对数组进行转置,最后显示转置后的数组 如何将元素指定给矩阵对象? 如何显示转置数组 package domain; public class Matrix { private int[][] numbers; public Matrix(int rows, int columns

我试图创建一个应用程序类MatrixApplication,用户首先在其中输入矩阵的行数和列数

这将用于创建数组对象。 然后逐行、逐列调用
矩阵的元素。读入所有元素后,它们将被指定给矩阵对象

接下来,对数组进行转置,最后显示转置后的数组

如何将元素指定给矩阵对象? 如何显示转置数组

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;
    }
}
包域;
公共类矩阵{
专用int[][]编号;
公共矩阵(整数行、整数列){
设置编号(编号);
如果(行数<1)
行=1;
其他的
行=行;
如果(列<1)
列=1;
其他的
列=列;
数字=新整数[行][列];
}
公共最终无效集合编号(int[][]编号){
这个。数字=数字;
}
public int[]getNumbers(){
返回号码;
}
公共int[][]转置(){
int[][]转置=新int[numbers[0].length][numbers.length];
对于(int i=0;i
packageui;
导入java.util.Scanner;
导入域矩阵;
公共类矩阵应用程序{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
System.out.print(“输入矩阵的行数:”);
int rows=input.nextInt();
System.out.print(“输入矩阵的列数:”);
int colums=input.nextInt();
矩阵=新矩阵(行、列);
最终整数[]个数=新整数[行][列];
对于(int i=0;i
如果我想要这种形式的转置矩阵:

只需将数字读入二维数组并调用
矩阵。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()));
final int[][]数字=新的int[行][列];
对于(int i=0;i
在打印转换矩阵之前,我调整了我的问题。你想看看吗?谢谢。@Leenieke19我提供的代码有什么问题?如果您只需要以不同的方式打印,请使用循环打印。
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()));