Page 719 - AP Computer Science A, 7th edition
P. 719
Section II
1. (a) public static void reverseArray(int[] arr) {
int mid = arr.length/2;
for (int i = 0; i < mid; i++) {
int temp = arr[i];
arr[i] = arr[arr.length – i – 1]; arr[arr.length – i – 1] = temp;
} }
(b) public void reverseAllRows() {
for (int[] row: mat) ArrayUtil.reverseArray (row);
}
(c) public void reverseMatrix() {
reverseAllRows();
int mid = mat.length/2;
for (int i = 0; i < mid; i++) {
for (int col = 0; col < mat[0].length; col++)
{
int temp = mat[i][col];
mat[i][col] = mat[mat.length – i – 1] [col];
mat[mat.length – i – 1][col] = temp;
} }
}
Alternative solution:
public void reverseMatrix() {