23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Many computer games, be they strategy games, simulation games, or first-person<br />

conflict games, use a two-dimensional "board." Programs that deal with such<br />

positional games need a way of represent<strong>in</strong>g objects <strong>in</strong> a two-dimensional space. A<br />

natural way to do this is with a two-dimensional array, where we use two <strong>in</strong>dices,<br />

say i <strong>and</strong> j, to refer to the cells <strong>in</strong> the array. The first <strong>in</strong>dex usually refers to a row<br />

number <strong>and</strong> the second to a column number. Given such an array we can then<br />

ma<strong>in</strong>ta<strong>in</strong> two-dimensional game boards, as well as perform other k<strong>in</strong>ds of<br />

computations <strong>in</strong>volv<strong>in</strong>g data that is stored <strong>in</strong> rows <strong>and</strong> columns.<br />

Arrays <strong>in</strong> <strong>Java</strong> are one-dimensional; we use a s<strong>in</strong>gle <strong>in</strong>dex to access each cell of an<br />

array. Nevertheless, there is a way we can def<strong>in</strong>e two-dimensional arrays <strong>in</strong> <strong>Java</strong>—<br />

we can create a two-dimensional array as an array of arrays. That is, we can def<strong>in</strong>e<br />

a two—dimensional array to be an array with each of its cells be<strong>in</strong>g another array.<br />

Such a two—dimensional array is sometimes also called a matrix. In <strong>Java</strong>, we<br />

declare a two—dimensional array as follows:<br />

<strong>in</strong>t[][] Y = new <strong>in</strong>t[8][10];<br />

This statement creates a two-dimensional "array of arrays," Y, which is 8 × 10,<br />

hav<strong>in</strong>g 8 rows <strong>and</strong> 10 columns. That is, Y is an array of length 8 such that each<br />

element of Y is an array of length 10 of <strong>in</strong>tegers. (See Figure 3.7.) The follow<strong>in</strong>g<br />

would then be valid uses of array Y <strong>and</strong> <strong>in</strong>t variables i <strong>and</strong> j:<br />

Y[i][i+1] = Y[i][i] + 3;<br />

i = a.length;<br />

j = Y[4].length;<br />

Two-dimensional arrays have many applications to numerical analysis. Rather than<br />

go<strong>in</strong>g <strong>in</strong>to the details of such applications, however, we explore an application of<br />

two-dimensional arrays for implement<strong>in</strong>g a simple positional game.<br />

Figure 3.7: Illustration of a two-dimensional <strong>in</strong>teger<br />

array, Y, which has 8 rows <strong>and</strong> 10 columns. The value of<br />

Y[3][5] is 100 <strong>and</strong> the value of Y[6][2] is 632.<br />

159

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!