Arbitrary definition of a two-dimensional array, to achieve the transpose of the Matrix--java

Source: Internet
Author: User

Import Java.util.Scanner;

/*

* Arbitrarily define a two-dimensional array, write a program to implement the Matrix transpose.

* */

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 ();

}

}

}

Arbitrary definition of a two-dimensional array, to achieve the transpose of the Matrix--java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.