7
Two-Dimensional Arrays Chapter 11

Chapter 11

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Chapter 11

Two-Dimensional Arrays

Chapter 11

Page 2: Chapter 11

11What 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.

Page 3: Chapter 11

11

Declare a two-dimensional array:int[][] anArray = new int[5][5];

Declaring a Two-Dimensional Array

Page 4: Chapter 11

11Iterating a Two-Dimensional

ArrayUse 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;}

}

Page 5: Chapter 11

11

Multidimensional ArraysArrays 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];

Page 6: Chapter 11

11Implementing Cellular

AutomataOne generationper row

One-dimensional array holds on/off status of each cell in each generation

Page 7: Chapter 11

11

The Game of LifeUsing two-dimensional arrays:

Entire board isone generation.Two-dimensionalarray holds state ofeach cell of the gameboard organized inrows and columns.