0
有沒有辦法讓我只用一個嵌套for循環執行第一個方法,而沒有第一個for循環一些值沒有翻轉,因爲你基本上正在執行逆x,y - - > y,x並翻轉x = y(因爲0,0是左上角)爲什麼我的嵌套for循環不能自行工作?2d方形陣列中的翻轉值
/**
* <pre>
* ~~~~~~ BONUS ~~~~~~ EXTRA CREDIT ~~~~~~
* transpose grid (similar to arrayReverse)
* (i.e. reflect over its main diagonal
* (which runs top-left to bottom-right)
* you MUST call the swap helper method
* but you man NOT create a second array for storage
* example:
* 1 2 3 1 4 7
* 4 5 6 would become 2 5 8
* 7 8 9 3 6 9
* </pre>
*/
public static void gridTranspose() {
if (gridIsSquare() == true) {
for (int r = 0; r < grid.length/2 + 1; r++) {
gridSwap(0, r, r, 0);
}
for (int r = 0; r < grid.length/2 + 1; r++) {
for (int c = 0; c < grid[0].length; c++) {
gridSwap(r, c, c, r);
}
}
}
}
/**
* <pre>
* ~~~~~~ BONUS ~~~~~~ EXTRA CREDIT ~~~~~~ swap the values at grid[r1][c1]
* and grid[r2][c2] you may use use an int temp as a temporary variable
*/
public static void gridSwap(int r1, int c1, int r2, int c2) {
int temp = grid[r1][c1];
grid[r1][c1] = grid[r2][c2];
grid[r2][c2] = temp;
}
您的代碼看起來不錯。什麼是問題? –
我只想知道是否有辦法用on打開我的第一個方法; y一個嵌套循環並消除循環的第一個方法。 – MickDom