我有一个基于行的多维数组:
/** [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