尝试这个。
static final int[][][] ROT = {
{{1, 0}, {0, 0}, {0, 1}, {0, 2}},
{{2, 0}, {2, 1}, {1, 1}, {0, 3}},
{{3, 0}, {2, 2}, {1, 2}, {1, 3}},
{{3, 1}, {3, 2}, {3, 3}, {2, 3}},
};
int[][] rotateArray(int[][] m) {
int[][] result = new int[m.length][m[0].length];
for (int row = 0; row < m.length; ++row)
for (int col = 0; col < m[0].length; ++col)
result[ROT[row][col][0]][ROT[row][col][1]] = m[row][col];
return result;
}和
int[][] m = {
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12},
{13, 14, 15, 16},
};
int[][] rotated = rotateArray(m);
for (int[] row : rotated)
System.out.println(Arrays.toString(row));结果:
[2, 3, 4, 8]
[1, 7, 11, 12]
[5, 6, 10, 16]
[9, 13, 14, 15]