Java Reference
In-Depth Information
b
b
b
B
R
B
R
r
r
Figure6-2BlackandRedPiecesonaCheckerboard
Sincethearrayindexiszero-basedtheindexvalue0representsthe
firstrow,andtheindexvalue2thethirdcolumn.Theotherpiecescanbe
storedinthecheckerboard[]array,asfollows:
checkerBoard[0][4] = 'b';
checkerBoard[1][3] = 'b';
checkerBoard[2][2] = 'B';
checkerBoard[3][1] = 'B';
checkerBoard[2][6] = 'R';
checkerBoard[4][4] = 'R';
checkerBoard[5][5] = 'r';
checkerBoard[5][7] = 'r';
AJavaprogramcouldnowusethecheckerboard[]arraytostorethe
positionsofallthepiecesasthegameprogresses.
Ragged arrays
MultidimensionalarraysareactuallyfakedbytheJavalanguage.Internally,
amultidimensionalarrayisimplementedasasetofone-dimensionalar-
rays.Forthisreasonatwo-dimensionalarraycanberectangularorragged
inshape,althoughrectangularonesaremuchmorecommon.Araggedar -
raycanbepicturedasin Figure6-3 .
 
Search WWH ::




Custom Search