Array application: Matrix transpose Java Edition

Source: Internet
Author: User

The program code is as follows:

Implementation of package chapter02;import java.util.arrays;/* *  matrix transpose (simple version)  */public class  Ch02_04 {public static void main (String[] args)  {int[][] arr = {{ 1, 2, 3},{4, 5, 6},{7, 8, 9}};int rows = arr.length;int cols  = arr[0].length;int[][] arrresult = new int[cols][rows];//fill result array for (int i  = 0; i < arrresult.length; i++) {Arrays.fill (arrResult[i], 0);} Matrix for (int i = 0; i < arr.length; i++) {System.out.println before the output matrix is transpose ( Arrays.tostring (Arr[i]));} Transpose The matrix for (int i = 0; i < rows; i++) {//outer loop, starting with the first line of the original matrix for (int j  = 0; j < cols; j++) {//inner loop, transpose matrix Arrresult[j][i] = arr[i][j];}} System.out.println ();//output Transpose matrix for (int i = 0; i < arrresult.length; i++) {System.ouT.println (Arrays.tostring (Arrresult[i]);}}} 

The results of the implementation are as follows:

[1, 2, 3] [4, 5, 6] [7, 8, 9] [1, 4, 7] [2, 5, 8] [3, 6, 9]


This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1976528

Array application: Matrix transpose Java Edition

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.