2D array practice exercises

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 AP CS Pictures Lab makes extensive use of 2D arrays.

2D array practice instructions

Download the 2D array practice skeleton code. Complete each method in the TwoDArrayPractice class.

Methods include:

  • Swapping 2 rows in a 2D array – This can be done both with and without traversing the rows.
  • Swapping 2 columns in a 2D array – The columns must be traversed.
  • Filling a 2D array in row-major order from a 1D data structure, in this case a string
  • Filling a 2D array in column-major order from a 1D data structure, in this case a 1D array
  • Filling a 2D array in down-up order (my term) from a 1D data structure
  • Growing a 2D array by constructing a new 2D array and copying elements to it
  • Cropping a 2D array by constructing a new 2D array to retain only elements within a specified range
  • Inverting a 2D array by constructing a new 2D array in which each row of the original array is a column

A test class has been provided that utilizes the JUnit framework. Many Java IDEs (including Eclipse and BlueJ) support JUnit.

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

Leave a Reply