集册 Java实例教程 创建矩阵并转置矩阵

创建矩阵并转置矩阵

欢马劈雪     最近更新时间:2020-01-02 10:19:05

465
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
创建矩阵并转置矩阵
/**from 时 代 J a v a - nowjava.com**/

public class Main {

  public static void main(String args[]) throws Exception {

    int row = 4;

    int col = 3;

    int[][] mMatrix = new int[row][col];

    int r, c;

    for (r = 0; r < mMatrix.length; r++) {

      for (c = 0; c < mMatrix[r].length; c++) {

        mMatrix[r][c] = r +c;

      }

    }

    // printing input Matrix :

    for (r = 0; r < mMatrix.length; r++) {

      for (c = 0; c < mMatrix[r].length; c++) {

        System.out.print(mMatrix[r][c]);

      }/** 时 代 J a v a 公 众 号 提供 **/

      System.out.println();

    }


    // transpose matrix declared

    int[][] mTranpose = new int[row][col];

    for (r = 0; r < mMatrix.length; r++) {

      for (c = 0; c < mMatrix[r].length; c++) {

        mTranpose[c][r] = mMatrix[r][c];

      }


    }


    System.out.println("Transpose :");

    for (r = 0; r < mMatrix.length; r++) {

      for (c = 0; c < mMatrix[r]
展开阅读全文