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: