从二维数组转置矩阵 [英] Transposing a matrix from a 2D array
问题描述
我自自学一些java和我卡上创建一个二维数组与随机值初始化它,然后创建数组的转置。
一个例子输出是:
$ java的测试1 22 333 555 44 6
输入行(1-10)的数量:0
错误:数字不在指定范围(1-10)!
依此类推,直到你进入行数和列数正确。
原始矩阵
1月22日
333 44
555 6
块引用>转置
1 333 555`
22 44 6`
块引用>^应该是最终的输出。与code一些帮助将AP preciated!
我想code产生的错误信息,如果行或列的数量是在指定范围之外。而对于如果读取命令行矩阵元素,而不是随机生成它们。
进口java.util.Scanner中;公共类的Test1 {
/ **主要方法* /
公共静态无效的主要(字串[] args){
扫描仪输入=新的扫描仪(System.in);
System.out.print(输入行的(1-10)的数量:);
INT行= input.nextInt();
System.out.print(输入栏的(1-10)的数量:);
INT COLS = input.nextInt(); //矩形二维数组创建originalMatrix
INT [] [] = originalMatrix新INT [行] [COLS]; //分配给originalMatrix随机值
对于(INT行= 0;&行LT; originalMatrix.length;排++)
对于(INT COL = 0;&山坳下,originalMatrix [行]。长度;西++){
originalMatrix [行] [COL] =(INT)(的Math.random()* 1000);
} //打印原始矩阵
的System.out.println(\\ nOriginal矩阵:);
printMatrix(originalMatrix); //置矩阵
INT [] [] = resultMatrix transposeMatrix(originalMatrix);
//打印转置矩阵
的System.out.println(\\ nTransposed矩阵:);
printMatrix(resultMatrix);
} / **用于打印矩阵的内容的方法,* /
公共静态无效printMatrix(INT [] []矩阵){
对于(INT行= 0;&行LT; matrix.length;排++){
对于(INT COL = 0;&山坳下,矩阵[行]。长度;西++){
System.out.print(矩阵[行] [COL] +);
}
的System.out.println();
}
} / **为转置矩阵*的方法/
公共静态INT [] [] transposeMatrix(INT [] []矩阵){
// code在这儿... ...
}
}
解决方案这是一个简单的方法,该方法返回转置矩阵的一个int [] [] ...
公共静态INT [] [] trasposeMatrix(INT [] []矩阵)
{
INT M = matrix.length;
INT N =矩阵[0]。长度; INT [] [] = trasposedMatrix新的INT [n] [M]; 对于(INT X = 0; X< N,X ++)
{
对于(INT Y = 0; Y&LT,M,Y ++)
{
trasposedMatrix [X] [Y] =矩阵[Y] [X]
}
} 返回trasposedMatrix;
}比打印二维矩阵,你可以使用这样的方法:
公共静态字符串matrixToString(INT [] []一)
{
INT M =则为a.length;
INT N = A [0]。长度; 字符串TMP =; 对于(INT Y = 0; Y&LT,M,Y ++)
{
对于(INT X = 0; X< N,X ++)
{
TMP = TMP +一个[Y] [X] +;
} TMP = TMP +\\ n;
} 返回TMP;
}I'm self teaching myself some java and I'm stuck on creating a 2D array that initializes it with random values and then creates the transpose of the array.
An example output is:
$ 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.
Original matrix
1 22 333 44 555 6
Transposed matrix
1 333 555` 22 44 6`
^ Should be the final output. Some help with the code would appreciated!
I would like to code to generate error messages if the number of rows or columns is outside the specified range. And for if to read the matrix elements from the command line and not generate them randomly.
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... } }
解决方案This is a simple method that return an int[][] of the trasposed matrix...
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; }
Than to print a 2D matrix you can use a method like this:
public static String matrixToString(int[][] a) { int m = a.length; int n = a[0].length; String tmp = ""; for(int y = 0; y<m; y++) { for(int x = 0; x<n; x++) { tmp = tmp + a[y][x] + " "; } tmp = tmp + "\n"; } return tmp; }
这篇关于从二维数组转置矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!