编写ArrayTransposition类,使用二维数组实现Matrix(矩阵)的转置 pta java基础练习7

编写程序题。 根据要求编写一个Java类,补充到代码中。

按要求编写ArrayTransposition类,通过二维数组行与列的互换,实现矩阵的转置。类中包含以下方法:

  1. inputMatrix方法:从键盘读入行数和列数,创建数组,然后从键盘输入数组的元素值

  2. transposition方法:互换二维数组的行与列。

方法声明如下:

int[][] inputMatrix();
int[][] transposition(int[][] array);

裁判测试程序样例:

import java.util.Scanner;
public class Main {
    //不改变main方法
    public static void main(String[] args) {
        ArrayTransposition trans = new ArrayTransposition();
        int[][] mat=trans.inputMatrix();    
        mat = trans.transposition(mat);    //转置数组
        display(mat);//打印转置后数组
    }

    //display方法:打印数组元素到屏幕
    public static void display(int[][] array){
        for(int i=0; i<array.length; i++){
            for(int j=0; j<array[i].length; j++){
                if(j==array[i].length-1) {
                    System.out.println(array[i][j]);
                }
                else {
                    System.out.print(array[i][j]+" ");
                }
            }
        }
    }
}

//补充ArrayTransposition类

    /* 请在这里填写答案 */

输入样例:

在这里给出一组输入。例如:

2 3
1 2 3
4 5 6

结尾无空行

输出样例:

在这里给出相应的输出。例如:

1 4
2 5
3 6

结尾无空行

class ArrayTransposition {
    Scanner sc = new Scanner(System.in);//注意在方法外创建Scanner对象来从键盘读入数据,注意关闭系统的输入流
    public int[][] inputMatrix() {
        int h = sc.nextInt();
        int l = sc.nextInt();
        int[][] arr = new int[h][l];
        for (int i = 0; i<h;i++){
            for (int j = 0;j<l;j++){
                arr[i][j] = sc.nextInt();
            }
        }
        return arr;
    }//从键盘读入一个数组,依次存放

    public int[][] transposition(int[][] mat) {
        int[][] ar = new int[mat[0].length][mat.length];
        for (int i = 0; i<mat[0].length;i++){
            for (int j = 0;j<mat.length;j++){
                ar[i][j] = mat[j][i];
            }
        }
        return ar;
    }//转置数组
}

本题还还是考察简单创建对象的方法,主要注意返回值类型就是了

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,请稍等... 以下是使用 Java 编写二维数组实现矩阵相乘与矩阵转置的源代码: 矩阵相乘: ``` public class MatrixMultiplication { public static void main(String[] args) { int[][] matrixA = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int[][] matrixB = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}}; int[][] result = multiply(matrixA, matrixB); printMatrix(result); } public static int[][] multiply(int[][] matrixA, int[][] matrixB) { int aRows = matrixA.length; int aCols = matrixA[0].length; int bRows = matrixB.length; int bCols = matrixB[0].length; if (aCols != bRows) { throw new IllegalArgumentException("The number of columns in matrix A must equal the number of rows in matrix B!"); } int[][] result = new int[aRows][bCols]; for (int i = 0; i < aRows; i++) { for (int j = 0; j < bCols; j++) { for (int k = 0; k < aCols; k++) { result[i][j] += matrixA[i][k] * matrixB[k][j]; } } } return result; } public static void printMatrix(int[][] matrix) { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } } ``` 矩阵转置: ``` public class MatrixTranspose { public static void main(String[] args) { int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int[][] result = transpose(matrix); printMatrix(result); } 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; } public static void printMatrix(int[][] matrix) { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } } ``` 希望能帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值