Difference between revisions of "Multidimensional arrays"

From Lazarus wiki
Line 2: Line 2:
  
 
You can have arrays in multiple dimensions:
 
You can have arrays in multiple dimensions:
<font color="#006699"><strong>type</strong></font>
+
<delphi>
    datatype <font color="#000000"><strong>=</strong></font> <font color="#006699"><strong>array</strong></font> <font color="#000000"><strong>[</strong></font>enum_type1<font color="#000000"><strong>,</strong></font> enum_type2<font color="#000000"><strong>]</strong></font> <font color="#006699"><strong>of</strong></font> datatype<font color="#000000"><strong>;</strong></font>
+
type
 +
  datatype = array [enum_type1, enum_type2] of datatype;
 +
</delphi>
  
 
The comma separates the dimensions, and referring to the array would be done with:
 
The comma separates the dimensions, and referring to the array would be done with:
a <font color="#000000"><strong>[</strong></font><font color="#ff0000">5</font><font color="#000000"><strong>,</strong></font> <font color="#ff0000">3</font><font color="#000000"><strong>]</strong></font>
+
<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:
 
Two-dimensional arrays are useful for programming board games. A tic tac toe board could have these type and variable declarations:
<font color="#006699"><strong>type</strong></font>
+
<delphi>
  StatusType <font color="#000000"><strong>=</strong></font> <font color="#000000"><strong>(</strong></font>X<font color="#000000"><strong>,</strong></font> O<font color="#000000"><strong>,</strong></font> Blank<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
type
  BoardType <font color="#000000"><strong>=</strong></font> <font color="#006699"><strong>array</strong></font><font color="#000000"><strong>[</strong></font><font color="#ff0000">1</font><font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font><font color="#ff0000">3</font><font color="#000000"><strong>,</strong></font><font color="#ff0000">1</font><font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font><font color="#ff0000">3</font><font color="#000000"><strong>]</strong></font> <font color="#006699"><strong>of</strong></font> StatusType<font color="#000000"><strong>;</strong></font>
+
  StatusType = (X, O, Blank);
<font color="#006699"><strong>var</strong></font>
+
  BoardType = array[1..3,1..3] of StatusType;
    Board <font color="#000000"><strong>:</strong></font> BoardType<font color="#000000"><strong>;</strong></font>
+
var
 +
  Board : BoardType;
 +
</delphi>
  
 
You could initialize the board with:
 
You could initialize the board with:
<font color="#006699"><strong>for</strong></font> count1 <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">1</font> <font color="#006699"><strong>to</strong></font> <font color="#ff0000">3</font> <font color="#006699"><strong>do</strong></font>
+
<delphi>
  <font color="#006699"><strong>for</strong></font> count2 <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">1</font> <font color="#006699"><strong>to</strong></font> <font color="#ff0000">3</font> <font color="#006699"><strong>do</strong></font>
+
for count1 := 1 to 3 do
    Board<font color="#000000"><strong>[</strong></font>count1<font color="#000000"><strong>,</strong></font> count2<font color="#000000"><strong>]</strong></font> <font color="#000000"><strong>:=</strong></font> Blank<font color="#000000"><strong>;</strong></font>
+
  for count2 := 1 to 3 do
 +
    Board[count1, count2] := Blank;
 +
</delphi>
 
You can, of course, use three- or higher-dimensional arrays.
 
You can, of course, use three- or higher-dimensional arrays.
  

Revision as of 17:00, 6 January 2010

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