Skip Top Navigation Bar

Introduction to Two-Dimensional Arrays

Sometimes data makes more sense if it is represented in a two-dimensional(2D) grid, instead of in a single one-dimensional list. Consider the following examples of data being stored in a 2D grid:

How Data is Stored?

An array of arrays can be used to represent a table of information. This is called a two-dimentional (2D) array.

Each value stored in an array is called an element. The size of the array is set when it is declared and created. This size does not change. So if more elements are needed a new array would need to be created, the current elements copied over, and the new elements added.

Each element in a 2D array has a row and column value. Row values start with 0 and end at the number of rows - 1. Column values also start at 0 and end at the number of columns - 1. When we draw out these diagrams, we traditionally label the rows down the left side, starting at 0 on the top, and the columns along the top, starting with 0 for the left most column, similar to the way rows and columns are layed out in a spreadsheet program.

Constructing a 2D Array

When constructing a 2D array object, we must specify the type of data that is stored. A 2D array can store either primitive or reference data.

General Format for Constructing an Array

<type of data>[][] nameOfArray = new <type of data>[<number of rows>][<number of columns>];

An example of a seating chart could be:

String[][] firstClass = new String[5][6];

Initial Values of an Array or 2D Array

When we create an array, some initial values are stored in the array.

Your Turn

Let's try it in the Java Playground.

Constructing a 2D Array with Initial Values

We can also create a 2D array and provide all the initial values. One way we can do this is by using an initializer list. This is good for when you have a shorter list of known values for an array. e values are listed in braces ({ }) and the array is assigned this list. Remember, a 2D array is an array made up of arrays, so your initializer list, will be comprised of initializer lists for each row of data. Th

    <type of data> [][] nameOfArray = { {<values for row 0>}, {<values for row 1>}, {<values for row 2>} };

NOTE: Not all rows have to have the same number of columns. For example:

int[][] numTimeAttended = { {3, 4}, {1, 7, 10}, {1}};

Produces the 2D Array:

3 4
1 7 10
1

Your Turn

Let's try it in the Java Playground.

Accessing Elements of a 2D Array

To access the elements of an array, use square brackets ([ ][ ]) and include the index value for the row and column in the square brackets.

<type of data>[][] name2DArray = { {<list of elements>}, {<list of elements>}, ... , {<list of elements>}};
<type of data> variable = name2DArray[<row index>][<col index>];

If we want to access the first element in row 0 and column 1, it would look as follows:

 int[][] gradeBook = { {80, 70, 90, 95}, {86, 73, 92, 92}, {83, 79, 90, 100} };
 int grade = gradeBook[0][1];

Your Turn

Let's try it in the Java Playground.

Determine the Number of Rows and Columns in a 2D Array

You can access the constant length to access the number of rows in the 2D array. To access the number of columns for each roow, you can use the constant lengthon an individual row.

int[][] gradeBook = { {80, 70, 90, 95}, {86, 73, 92, 92}, {83, 79, 90, 100} };
int numRows = gradeBook.length;
int numCols = gradeBook[0].length;

Your Turn

Let's try it in the Java Playground.

Resources

To come