English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
في هذا البرنامج، ستتعلم البحث عن طباعة تحويل المصفوفة المقدمة في Java.
تحويل مصفوفة هو عملية تبديل الصفوف إلى أعمدة. بالنسبة لمصفوفة 2x3،
المصفوفة a11 a12 a13 a21 a22 a23 تحويل المصفوفة a11 a21 a12 a22 a13 a23
public class Transpose { public static void main(String[] args) { int row = 2, column = 3; int[][] matrix = {{2, 3, 4}, {5, 6, 4}}; //显示当前的矩阵 display(matrix); //转置矩阵 int[][] transpose = new int[column][row]; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { transpose[j][i] = matrix[i][j]; } } //显示转置矩阵 display(transpose); } public static void display(int[][] matrix) { System.out.println("The matrix is: "); for (int[] row : matrix) { for (int column : row) { System.out.print(column " "); } System.out.println(); } } }
When running the program, the output is:
The matrix is: 2 3 4 5 6 4 The matrix is: 2 5 3 6 4 4
In the above program, the display() function is only used to print the content of the matrix to the screen.
Here, the given matrix is in the form of 2x3, that is, row = 2 and column = 3.
For the transpose matrix, we change the transpose order to 3x2, that is, row = 3 and column = 2. So, we have transpose = int[column][row]
The transpose of the matrix is calculated by simply swapping columns for rows:
transpose[j][i] = matrix[i][j];