Presentation is loading. Please wait.

Presentation is loading. Please wait.

© The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays.

Similar presentations


Presentation on theme: "© The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays."— Presentation transcript:

1 © The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays

2 © The McGraw-Hill Companies, 2006 Two-dimensional arrays a multi-dimensional array is an array that has more than one index; so far, the arrays that we have shown you have had only one index - for this reason they are very often referred to as one-dimensional arrays; however, an array may have as many indices as is necessary (up to the limit of the memory on your machine); usually, no more than two indices will ever need to be used; an array with two indices is called a two- dimensional array.

3 © The McGraw-Hill Companies, 2006 Creating a two-dimensional array To create a two-dimensional (2D) array, simply provide the size of both indices. Example Assume we need to record temperatures for four weeks - each week will itself consist of seven temperatures (one for each day of the week):

4 © The McGraw-Hill Companies, 2006 Accessing elements in a 2D array The name of each item in a two-dimensional array is the array name, plus the row and column index.

5 © The McGraw-Hill Companies, 2006 Use a pair of nested loops to process a 2D array

6 © The McGraw-Hill Companies, 2006 Revisiting the 'length' attribute in a 2D array, the length attribute returns the length of the first index (this is what we have visualised as a the number of rows); the number of columns is determined by obtaining the length of a particular row; in the previous example we have chosen the first row but we could have chosen any row here.

7 © The McGraw-Hill Companies, 2006 Initializing 2D arrays As with one dimensional arrays, it is possible to declare and initialize a multi-dimensional array with a collection of values all in one line. Example

8 © The McGraw-Hill Companies, 2006 Ragged arrays A two-dimensional array with a variable number of columns is called a ragged array. Example

9 © The McGraw-Hill Companies, 2006 Creating a ragged array to declare such a ragged array we first need to specify the number of rows and leave the number of columns unspecified: now, for each row we can fix the appropriate size of the associated column; in the example above the first row has 2 columns, the second row 4 columns.

10 © The McGraw-Hill Companies, 2006 The NoughtsAndCrosses game

11 © The McGraw-Hill Companies, 2006

12 Creating a 2D array of buttons A 2D array of buttons would be a good way to implement the Noughts and Crosses board.

13 © The McGraw-Hill Companies, 2006 Creating the buttons Now we need to allocate a new JButton object to each cell in the array ; give each button an actionListener and then to add each button to a panel (called board say).

14 © The McGraw-Hill Companies, 2006 Processing each button press

15 © The McGraw-Hill Companies, 2006 Check if player has won There are four ways in which a player can win a game: the player completes a row; the player completes a column; the player completes the left diagonal; the player completed the right diagonal.

16 © The McGraw-Hill Companies, 2006 Completing a row When a player wins the game by completing a row, the row index for each winning cell remains the same.

17 © The McGraw-Hill Companies, 2006 Code for checking a column If a player wins a game by completing a column, then the column index will be fixed to the current column (colIn):

18 © The McGraw-Hill Companies, 2006 Completing a left diagonal When a player has won the game by completing a left diagonal, the row and column index for each winning cell is the same.

19 © The McGraw-Hill Companies, 2006 Code for checking a left diagonal

20 © The McGraw-Hill Companies, 2006 Code for right diagonal When checking the right diagonal, the cells to check are cell[2][0], cell[1][1] and cell[0][2]. This row index can be arrived at by subtracting the column index from 2.


Download ppt "© The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays."

Similar presentations


Ads by Google