任意定义一个二维数组,实现矩阵的转置——java

import java.util.Scanner;

/*

 * 任意定义一个二维数组,编写程序实现矩阵的转置。

 * */

public class MatrixTransposition {

 

  public static void main(String[] args) {

    //create the rowNumber and columnNumber of the matrix

    Scanner input = new Scanner(System.in);

    System.out.println("input the rowNumber and columnNumber");

    int rowNumber = input.nextInt();

    int columnNumber = input.nextInt();

 

    //create the matrix

    int matrix[][] = createMatrix(rowNumber,columnNumber);//deliver the rowNumber and columnNumber

    outputMatrix(matrix);

 

    System.out.println("the Transform result is :");

    int transitionResult[][] = transformMatrix(matrix);

 

    //input matrix

    outputMatrix(transitionResult);

}

 

    //create matrix

  public static int[][] createMatrix(int x,int y)

  {

    Scanner input = new Scanner(System.in);

    //input the datas;

    System.out.println("please the datas");

    int array[][] = new int[x][y];

    for(int i = 0;i < array.length;i ++)

    {

      for(int j = 0;j < array[i].length;j++)

      {

        array[i][j] = input.nextInt();

      }

    }

    return array;  

  }

 

  //tranform matrix

  public static int[][] transformMatrix(int matrix[][])

  {

    int a[][] = new int[matrix[0].length][matrix.length];

    for(int i = 0;i < matrix[0].length;i++)

    {

      for(int j = 0;j < matrix.length;j++)

      {

        a[i][j] = matrix[j][i];

      }

    }

    return a;

  }

 

 

  //output matrix

  public static void outputMatrix(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();

    }

  }

 

}

posted @ 2015-05-27 08:44  天&泽  阅读(11098)  评论(0编辑  收藏