Java programming to implement the two-dimensional array transpose function example, java two-dimensional array

Source: Internet
Author: User

Java programming to implement the two-dimensional array transpose function example, java two-dimensional array

This example describes the two-dimensional array transpose function implemented by Java programming. We will share this with you for your reference. The details are as follows:

/*** Implement transpose of two-dimensional arrays * @ author HAN **/public class transposition_Arrays2D_ch6_4 {final static double PI = 3.1415; public static void main (String [] args) {/* StaticTest st1 = new StaticTest (); StaticTest st2 = new StaticTest (); st1.method2 ("HAN "); * // ***** defines the two-dimensional array to be used for transpose ******/int arr2D [] [] ={{, 3, 6 },{, 9 }};/****** new two-dimensional array of construction results used to store the transpose result ****** // * defines the result array variable, note that you must first open up a memory; otherwise, only the address is passed, that is, the two array names actually point to the same memory */// The construction of a two-dimensional array can be performed for dimensions, not necessarily a matrix, that is, the length of each row is not necessarily the same int result_arr [] [] = new int [arr2D. length] []; // first implement the first dimension for (int I = 0; I <arr2D. length; I ++) {// implements the second-dimensional result_arr [I] = new int [arr2D [I]. length];} // int result_arr [] [] = Arrays. copyOf (arr2D, arr2D. length); // The above command line does not work! /***** Output the two-dimensional array for transpose ******/for (int x []: arr2D) {for (int e: x) {System. out. print (e + "");} System. out. println ();} System. out. println ();/******* element inversion *******/for (int I = 0; I <arr2D. length; I ++) {for (int j = 0; j <arr2D [I]. length; j ++) {result_arr [j] [I] = arr2D [I] [j]; // transpose core}/***** show the result in the result matrix ********/for (int x []: result_arr) {for (int e: x) {System. out. print (e + "");} System. out. println () ;}}// import java. util. arrays; // public class transposition_Arrays2D {// public static void main (String [] args) {// int arr2D [] [] = {1, 2, 3 }, {4, 5, 6}, {7, 8, 9}; // * defines the result array variable. Note that you must first open up a memory. // otherwise, the variable is only transmitted by address, that is to say, the two array names actually point to the same memory * // int result_arr [] [] = new int [arr2D. length] []; // for (int I = 0; I <arr2D. length; I ++) {// result_arr [I] = new int [arr2D [I]. length]; //} // element inversion // for (int I = 0; I <arr2D. length; I ++) {// for (int j = 0; j <arr2D [I]. length; j ++) {// result_arr [j] [I] = arr2D [I] [j]; ///} // show the result in matrix // for (int x []: result_arr) {// for (int e: x) {// System. out. print (e); //} // System. out. println ();//}////}////}

Running result:

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.