Multidimensional arrays

From Lazarus wiki
Revision as of 16:45, 21 November 2007 by Kees (talk | contribs) (New page: Multidimensional Arrays You can have arrays in multiple dimensions: <font color="#006699"><strong>type</strong></font> datatype <font color="#000000"><strong>=</strong></font> <font ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Multidimensional Arrays

You can have arrays in multiple dimensions:

type
   datatype = array [enum_type1, enum_type2] of datatype;

The comma separates the dimensions, and referring to the array would be done with:

a [5, 3]

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

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

You could initialize the board with:

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

You can, of course, use three- or higher-dimensional arrays.

previous contents next