Two-Dimensional
Arrays
Chapter 11
11
What is a two-dimensional array?
A two-dimensional
array has “rows”
and “columns,”
and can be thought
of as a series of
one-dimensional
arrays stacked on
top of one another.
11
Declare a two-dimensional array:
int[][] anArray = new int[5][5];
Declaring a Two-Dimensional Array
11
Iterating a Two-Dimensional Array
Use a nested for loop:
Outer loop iterates the row
Inner loop iterates the column.
for ( int i=0; i<5; i++ )
{
for ( int j=0; j<5; j++ )
{
anArray[i][j] = i+j;
}
}
11
Multidimensional Arrays
Arrays can have more than two
dimensions.
Multidimensional arrays are not
commonly used as they quickly become
difficult to manage.
Declare multidimensional array:
int[][][] anArray = new
int[5][5][5];
11
Implementing Cellular Automata
One generation
per row
One-dimensional
array holds on/off
status of each cell
in each generation
11
The Game of Life
Using two-dimensional arrays:
Entire board is
one generation.
Two-dimensional
array holds state of
each cell of the game
board organized in
rows and columns.

Chapter 11

  • 1.
  • 2.
    11 What is atwo-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series of one-dimensional arrays stacked on top of one another.
  • 3.
    11 Declare a two-dimensionalarray: int[][] anArray = new int[5][5]; Declaring a Two-Dimensional Array
  • 4.
    11 Iterating a Two-DimensionalArray Use a nested for loop: Outer loop iterates the row Inner loop iterates the column. for ( int i=0; i<5; i++ ) { for ( int j=0; j<5; j++ ) { anArray[i][j] = i+j; } }
  • 5.
    11 Multidimensional Arrays Arrays canhave more than two dimensions. Multidimensional arrays are not commonly used as they quickly become difficult to manage. Declare multidimensional array: int[][][] anArray = new int[5][5][5];
  • 6.
    11 Implementing Cellular Automata Onegeneration per row One-dimensional array holds on/off status of each cell in each generation
  • 7.
    11 The Game ofLife Using two-dimensional arrays: Entire board is one generation. Two-dimensional array holds state of each cell of the game board organized in rows and columns.