java多维数组转置

我有一个基于行的多维数组:

/** [row][column]. */ public int[][] tiles; 

我想将此数组转换为基于列的数组,如下所示:

 /** [column][row]. */ public int[][] tiles; 

……但我真的不知道从哪里开始

尝试这个:

 @Test public void transpose() { final int[][] original = new int[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; for (int i = 0; i < original.length; i++) { for (int j = 0; j < original[i].length; j++) { System.out.print(original[i][j] + " "); } System.out.print("\n"); } System.out.print("\n\n matrix transpose:\n"); // transpose if (original.length > 0) { for (int i = 0; i < original[0].length; i++) { for (int j = 0; j < original.length; j++) { System.out.print(original[j][i] + " "); } System.out.print("\n"); } } } 

输出:

 1 2 3 4 5 6 7 8 9 10 11 12 matrix transpose: 1 5 9 2 6 10 3 7 11 4 8 12 

我看到所有答案都创建了一个新的结果矩阵。 这很简单: matrix[i][j] = matrix[j][i]; 但是,在方形矩阵的情况下,您也可以就地执行此操作。

 // Transpose, where m == n for(int i = 0; i < m; i++) { for(int j = i+1; j < n; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } 

这对于较大的矩阵更好,其中创建新的结果矩阵在存储器方面是浪费的。 如果它不是方形,您可以创建一个具有NxM尺寸的新方法并执行不合适的方法。 注意:对于就地,请注意j = i + 1; 它不是0。

我正在挖掘这个post,因为我没有在答案中找到一个有效的解决方案,因此我会发布一个来帮助搜索一个人的人:

 public int[][] transpose (int[][] array) { if (array == null || array.length == 0)//empty or unset array, nothing do to here return array; int width = array.length; int height = array[0].length; int[][] array_new = new int[height][width]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { array_new[y][x] = array[x][y]; } } return array_new; } 

你应该通过以下方式调用它:

 int[][] a = new int[][] {{1,2,3,4},{5,6,7,8}}; for (int i = 0; i < a.length; i++) { System.out.print("["); for (int y = 0; y < a[0].length; y++) { System.out.print(a[i][y] + ","); } System.out.print("]\n"); } a = transpose(a); // call System.out.println(""); for (int i = 0; i < a.length; i++) { System.out.print("["); for (int y = 0; y < a[0].length; y++) { System.out.print(a[i][y] + ","); } System.out.print("]\n"); } 

这将按预期输出:

 [1,2,3,4,] [5,6,7,8,] [1,5,] [2,6,] [3,7,] [4,8,] 

更通用的方式:

 /** * Transposes the given array, swapping rows with columns. The given array might contain arrays as elements that are * not all of the same length. The returned array will have {@code null} values at those places. * * @param  * the type of the array * * @param array * the array * * @return the transposed array * * @throws NullPointerException * if the given array is {@code null} */ public static  T[][] transpose(final T[][] array) { Objects.requireNonNull(array); // get y count final int yCount = Arrays.stream(array).mapToInt(a -> a.length).max().orElse(0); final int xCount = array.length; final Class componentType = array.getClass().getComponentType().getComponentType(); @SuppressWarnings("unchecked") final T[][] newArray = (T[][]) Array.newInstance(componentType, yCount, xCount); for (int x = 0; x < xCount; x++) { for (int y = 0; y < yCount; y++) { if (array[x] == null || y >= array[x].length) break; newArray[y][x] = array[x][y]; } } return newArray; } 

如果你想进行矩阵的转置(在这种情况下, row count = col count )你可以在Java中跟随

 public static void inPlaceTranspose(int [][] matrix){ int rows = matrix.length; int cols = matrix[0].length; for(int i=0;i 
 import java.util.Arrays; import java.util.Scanner; public class Demo { public static void main(String[] args) { Scanner input = new Scanner(System.in); int rows = askArray("Enter number of rows :", input);//asking number of rows from user int columns = askArray("Enter number of columns :", input);//asking number of columns from user int[][] array = Array(rows, columns, input); DisplayArray(array, rows, columns);//displaying initial matrix System.out.println("Transpose array "); int[][] array2=TransposeArray(array, rows,columns );//calling Transpose array method for (int i = 0; i < array[0].length; i++) { System.out.println(Arrays.toString(array2[i])); } } //method to take number of rows and number of columns from the user public static int askArray(String s, Scanner in) { System.out.print(s); int value = in.nextInt(); return value; } //feeding elements to the matrix public static int[][] Array(int x, int y, Scanner input) { int[][] array = new int[x][y]; for (int j = 0; j < x; j++) { System.out.print("Enter row number " + (j + 1) + ":"); for (int i = 0; i < y; i++) { array[j][i] = input.nextInt(); } } return array; } //Method to display initial matrix public static void DisplayArray(int[][] arra, int x, int y) { for (int i = 0; i < x; i++) { System.out.println(Arrays.toString(arra[i])); } } //Method to transpose matrix public static int[][] TransposeArray(int[][] arr,int x,int y){ int[][] Transpose_Array= new int [y][x]; for (int i = 0; i < x; i++) { for (int j = 0; j  
  public int[][] getTranspose() { int[][] transpose = new int[row][column]; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { transpose[i][j] = original[j][i]; } } return transpose; } 
 public int[][] tiles, temp; // Add values to tiles, wherever you end up doing that, then: System.arraycopy(tiles, 0, temp, 0, tiles.length); for(int row = 0; row < tiles.length; row++) // Loop over rows for(int col = 0; col < tiles[row].length; col++) // Loop over columns tiles[col][row] = temp[row][col]; // Rotate 

那应该为你做。