Two Dimensional Arrays

This chapter expands our discussion on one-dimensional arrays to two dimensional arrays. A two dimensional array is a data structure that contains a collection of cells laid out in a two dimensional grid, similar to a table with rows and columns although the values are still stored linearly in memory. Each cell in a two dimensional array can be accessed through two indexes that specify the row number and column number respectively. Like s one dimensional array, the range of each index is from zero to the size of the row or column minus one. A nested for loop is commonly used to initialize, manipulate, and access the values in a two dimensional array.

1. Two Dimensional Arrays

Often data comes naturally in two dimensional form. Web browser screens are two dimensional, as are spreadsheets, printed pages, and images.

For many of 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.

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

One programming problem in the 2014 Advanced Placement test for computer science required use of a two dimensional array of object references.

Chapter Topics:

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

Question 1:

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


Source: Bradley Kjell, http://programmedlessons.org/Java9/chap67/ch67_01.html
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 License.