Basic Pascal Tutorial/Chapter 5/Multidimensional arrays

From Lazarus wiki
Revision as of 16:00, 6 January 2010 by Kees (talk | contribs)
Jump to navigationJump to search

5D - Multidimensional Arrays (author: Tao Yue, state: unchanged)

You can have arrays in multiple dimensions: <delphi> type

 datatype = array [enum_type1, enum_type2] of datatype;

</delphi>

The comma separates the dimensions, and referring to the array would be done with: <delphi> a [5, 3] </delphi>

Two-dimensional arrays are useful for programming board games. A tic tac toe board could have these type and variable declarations: <delphi> type

 StatusType = (X, O, Blank);
 BoardType = array[1..3,1..3] of StatusType;

var

 Board : BoardType;

</delphi>

You could initialize the board with: <delphi> for count1 := 1 to 3 do

 for count2 := 1 to 3 do
   Board[count1, count2] := Blank;

</delphi> You can, of course, use three- or higher-dimensional arrays.

previous contents next