从2Darrays转置矩阵

我自学了一些java,我一直在创建一个用随机值初始化它的2D数组,然后创建数组的转置。

示例输出是:

$ java Test1 22 333 44 555 6 Enter the number of rows (1-10): 0 ERROR: number not in specified range (1-10) ! and so on until you enter the correct number of rows and columns. 

原始矩阵

  1 22 333 44 555 6 

转置矩阵

  1 333 555` 22 44 6` 

^应该是最终输出。 一些帮助代码将不胜感激!

如果行数或列数超出指定范围,我想编码生成错误消息。 并且if是从命令行读取矩阵元素而不是随机生成它们。

 import java.util.Scanner; public class Test1 { /** Main method */ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of rows (1-10): "); int rows = input.nextInt(); System.out.print("Enter the number of columns (1-10): "); int cols = input.nextInt(); // Create originalMatrix as rectangular two dimensional array int[][] originalMatrix = new int[rows][cols]; // Assign random values to originalMatrix for (int row = 0; row < originalMatrix.length; row++) for (int col = 0; col < originalMatrix[row].length; col++) { originalMatrix[row][col] = (int) (Math.random() * 1000); } // Print original matrix System.out.println("\nOriginal matrix:"); printMatrix(originalMatrix); // Transpose matrix int[][] resultMatrix = transposeMatrix(originalMatrix); // Print transposed matrix System.out.println("\nTransposed matrix:"); printMatrix(resultMatrix); } /** The method for printing the contents of a matrix */ public static void printMatrix(int[][] matrix) { for (int row = 0; row < matrix.length; row++) { for (int col = 0; col < matrix[row].length; col++) { System.out.print(matrix[row][col] + " "); } System.out.println(); } } /** The method for transposing a matrix */ public static int[][] transposeMatrix(int[][] matrix) { // Code goes here... } } 

这是一个返回trasposed矩阵的int [] []的简单方法…

 public static int[][] trasposeMatrix(int[][] matrix) { int m = matrix.length; int n = matrix[0].length; int[][] trasposedMatrix = new int[n][m]; for(int x = 0; x < n; x++) { for(int y = 0; y < m; y++) { trasposedMatrix[x][y] = matrix[y][x]; } } return trasposedMatrix; } 

要打印2D矩阵,您可以使用如下方法:

 public static String matrixToString(int[][] a) { int m = a.length; int n = a[0].length; String tmp = ""; for(int y = 0; y 

上面提供的答案在内存方面效率不高。 它正在使用另一个数组 – transposedMatrix除了作为参数提供的数组。 这将导致消耗双倍内存。 我们可以按如下方式就地执行此操作:

 public void transposeMatrix(int[][] a) { int temp; for(int i=0 ; i<(a.length/2 + 1); i++) { for(int j=i ; j<(a[0].length) ; j++) { temp = a[i][j]; a[i][j] = a[j][i]; a[j][i] = temp; } } displayMatrix(a); } public void displayMatrix(int[][] a){ for(int i=0 ; i 

您可以使用下面的类,它具有您想要的大多数方法。

 /** * Class representing square matrix of given size. * It has methods to rotate by 90, 180 and 270 * And also to transpose and flip on either axis. * * I have used both space efficient methods in transpose and flip * And simple but more space usage for rotation. * * This is using builder pattern hence, you can keep on applying * methods say rotate90().rotate90() to get 180 turn. * */ public class Matrix { private int[][] matrix; final int size; public Matrix(final int size) { this.size = size; matrix = new int[size][size]; for (int i=0;i 3) { sb.append("\t"); } } sb.append("|\n"); } return sb.toString(); } public static void main(String... args) { Matrix m = new Matrix(3); System.out.println(m); //transpose and flipHorizontal is 270 turn (-90) System.out.println(m.transpose()); System.out.println(m.flipHorizontal()); //rotate 90 further to bring it back to original position System.out.println(m.rotate90()); //transpose and flip Vertical is 90 degree turn System.out.println(m.transpose().flipVertical()); } } 

输出:

 |0|1|2| |3|4|5| |6|7|8| |0|3|6| |1|4|7| |2|5|8| |2|5|8| |1|4|7| |0|3|6| |0|1|2| |3|4|5| |6|7|8| |6|3|0| |7|4|1| |8|5|2| 

对于方形矩阵,您只需遍历2D数组的对角线一半,并使用相应的索引交换值,而不是遍历整个数组。

 public void transposeMatrix(int[][] a) { for(int i=0 ; i