# 2D array practice exercises

See Intro to 2D arrays before attempting the exercises below.

Increasingly complex manipulations of 2D arrays have been featured on the AP Computer Science A Exam. This exercise includes traversals, swaps, and other manipulations of 2 dimensional arrays.

The Java files below include skeleton code for each method and a JUnit 5 tester for each method. See Running JUnit 5 tests.

## `rowSwap` method

``````/**
* Swaps all values in the specified 2 rows of mat.
* @param mat the array
* @param rowAIndex the index of a row to be swapped
* @param rowBIndex the index of a row to be swapped
*/
public static void rowSwap(int[][] mat, int rowAIndex, int rowBIndex)
``````

### `rowSwap` example

``````mat (before call):
10  9   8   7
6   5   4   3
2   1   -1  0

rowSwap(mat, 1, 2);

mat (after call):
10  9   8   7
2   1   -1  0
6   5   4   3
``````

## `colSwap` method

``````/**
* Swaps all values in the specified 2 columns of mat.
* @param mat the array
* @param colAIndex the index of a column to be swapped
* @param colBIndex the index of a column to be swapped
*/
public static void colSwap(int[][] mat, int colAIndex, int colBIndex)
``````

### `colSwap` example

``````mat (before call):
10  9   8   7
6   5   4   3
2   1   -1  0

colSwap(mat, 1, 3);

mat (after call):
10  7   8   9
6   3   4   5
2   0   -1  1
``````

## `fillRowMajor` method

``````/**
* Returns an array with the specified number of rows and columns
* containing the characters from str in row-major order. If str.length()
* is greater than rows * cols, extra characters are ignored. If
* str.length() is less than rows * cols, the remaining elements in the
* returned array contain null.
*
* @param str the string to be placed in an array
* @param rows the number of rows in the array to be returned
* @param cols the number of columns in the array to be returned
* @return an array containing the characters from str in row-major order
*/
public static String[][] fillRowMajor(String str, int rows, int cols)
``````

### `fillRowMajor` example 1

``````String[][] result = fillRowMajor("stop whining", 3, 4);

result (after method call):
"s"     "t"     "o"     "p"
" "     "w"     "h"     "i"
"n"     "i"     "n"     "g"
``````

### `fillRowMajor` example 2

``````String[][] result = fillRowMajor("stop complaining", 3, 4);

result (after method call):
"s"     "t"     "o"     "p"
" "     "c"     "o"     "m"
"p"     "l"     "a"     "i"
``````

### `fillRowMajor` example 3

``````String[][] result = fillRowMajor("do more", 3, 4);

result (after method call):
"d"     "o"     " "     "m"
"o"     "r"     "e"     null
null    null    null    null
``````

## `fillColumnMajor` method

``````/**
* Returns an array containing the elements of vals in column-major order.
*
* Precondition: vals.length == rows * cols
*
* @param vals the elements
* @param rows the number of rows in the array to be returned
* @param cols the number of columns in the array to be returned
* @return an array containing the elements of vals in column-major order
*/
public static int[][] fillColumnMajor(int[] vals, int rows, int cols)
``````

### `fillColumnMajor` example

``````int[] source = new int[] {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1};

int[][] result = fillColumnMajor(source, 3, 4);

result (after method call):
10  7   4   1
9   6   3   0
8   5   2   -1
``````

## `fillDownUp` method

``````/**
* Returns an array with the specified number of rows and columns that
* contains the elements of vals in the order specified below. Elements
* from vals are placed in the array by moving down the first column,
* up the second column and so on.
*
* Precondition: vals.length == rows * cols
*
* @param vals the elements
* @param rows the number of rows in the array to be returned
* @param cols the number of columns in the array to be returned
* @return an array containing the elements from vals in the specified order
*/
public static int[][] fillDownUp(int[] vals, int rows, int cols)
``````

### `fillDownUp` example

``````int[] source = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

int[][] result = fillDownUp(source, 3, 4);

result (after method call):
1   6   7   12
2   5   8   11
3   4   9   10
``````

## `grow` method

``````/**
* Returns a larger array containing the same elements as the
* mat. The elements from mat are read in row-major order and
* appear in the new array in row-major order.
*
* Precondition: newRows > mat.length && newCols > mat[0].length
*
* @param mat the 2D array containing the original elements
* @param newRows the number of rows in the new array
* @param newCols the number of columns in the new array
* @return a larger array containing the elements from mat in row-major order
*/
public static int[][] grow(int[][] mat, int newRows, int newCols)
``````

### `grow` example

``````mat (before method call):
10  9   8   7
6   5   4   3
2   1   -1  0

int[][] result = grow(mat, 4, 5);

result (after method call):
10  9   8   7   6
5   4   3   2   1
-1  0   0   0   0
0   0   0   0   0
``````

## `crop` method

``````/**
* Returns a smaller array containing the elements in the specified
* range of the mat.
*
* @param mat the 2D array containing the original elements
* @param startRow the first row to be kept
* @param startCol the first column to be kept
* @param endRow the last row to be kept
* @param endCol the last column to be kept
* @return a smaller array containing the specified elements
*/
public static int[][] crop(int[][] mat,
int startRow, int startCol,
int endRow, int endCol)
``````

### `crop` example

``````mat (before method call):
10  9   8   7
6   5   4   3
2   1   -1  0

int[][] result = crop(mat, 0, 1, 1, 2);

result (after method call):
9   8
5   4
``````

## `invert` method

``````/**
* Returns an array containing the same elements as
* mat but with the rows and columns reversed.
*
* @param mat the array
* @return an array containing elements as described
*/
public static int[][] invert(int[][] mat)
``````

### `invert` example

``````mat (before method call);
10  9   8   7
6   5   4   3
2   1   -1  0

int[][] result = invert(mat);

result (after method call):
10  6   2
9   5   1
8   4   -1
7   3   0
``````

## `consolidate` method

``````/**
* Consolidates the non-null elements in mat such
* that they appear in row-major order, starting at
* the top left.
*/
public static void consolidate(String[][] mat)
``````

### `consolidate` example 1

``````mat (before method call):
"a"     null    "b"     "c"     null
null    "d"     null    null    null
"e"     null    null    "f"     null

consolidate(mat);

mat (after method call):
"a"     "b"     "c"     "d"     "e"
"f"     null    null    null    null
null    null    null    null    null
``````

### `consolidate` example 2

``````mat (before method call):
"a"     null    "b"     "c"     null
null    "d"     "e"     null    "f"
"g"     "h"     "i"     "j"     "k"

consolidate(mat);

mat (after method call):
"a"     "b"     "c"     "d"     "e"
"f"     "g"     "h"     "i"     "j"
"k"     null    null    null    null
``````

## Solution & comments

See the 2D array practice solutions or review them with AP CS Tutor Brandon Horn.

Comment on 2D array practice exercises