CHAPTER 49C — Two-Dimensional Arrays

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to home page   go to next page

created: 12/03/99; revised: 01/24/00, 07/20/02, 05/30/03, 02/03/06

CHAPTER 49C — Two-Dimensional Arrays

Often data comes naturally in a two dimensional form. For example, maps are two dimensional (or more), the layout of a printed page is two dimensional, a computer-generated image (such as on your computer screen) is two dimensional, and so on.

For these situations you need a two-dimensional array. This is a collection of cells laid out in a 2D grid, like graph paper. Each cell can hold a value (as with a 1D array). However, now two indexes are needed to specify a cell.

Chapter Topics:

  • Two-dimensional Arrays
  • 2D Array Declaration
  • Initializer Lists
  • Differing Numbers of cells in each row
  • 2D Arrays as Arrays of 1D Arrays
  • Printing a 2D Array

If you have ever used a spreadsheet application then you already have used a 2D grid for holding and manipulating data.

QUESTION 1:

(Thought question: ) Do you suspect that 2D arrays in Java are objects?