Difference between revisions of "Multidimensional arrays"

From Lazarus wiki
m (Fixed syntax highlighting)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
{{Multidimensional arrays}}
 +
{{TYNavigator|1-dimensional_arrays|Records}}
 +
 
5D - Multidimensional Arrays (author: Tao Yue, state: unchanged)
 
5D - Multidimensional Arrays (author: Tao Yue, state: unchanged)
  
 
You can have arrays in multiple dimensions:
 
You can have arrays in multiple dimensions:
<delphi>
+
 
 +
<syntaxhighlight lang=pascal>
 
type
 
type
 
   datatype = array [enum_type1, enum_type2] of datatype;
 
   datatype = array [enum_type1, enum_type2] of datatype;
</delphi>
+
</syntaxhighlight>
  
 
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:
<delphi>
+
 
 +
<syntaxhighlight lang=pascal>
 
a [5, 3]
 
a [5, 3]
</delphi>
+
</syntaxhighlight>
  
 
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:
<delphi>
+
 
 +
<syntaxhighlight lang=pascal>
 
type
 
type
 
   StatusType = (X, O, Blank);
 
   StatusType = (X, O, Blank);
Line 19: Line 25:
 
var
 
var
 
   Board : BoardType;
 
   Board : BoardType;
</delphi>
+
</syntaxhighlight>
  
 
You could initialize the board with:
 
You could initialize the board with:
<delphi>
+
 
 +
<syntaxhighlight lang=pascal>
 
for count1 := 1 to 3 do
 
for count1 := 1 to 3 do
 
   for count2 := 1 to 3 do
 
   for count2 := 1 to 3 do
 
     Board[count1, count2] := Blank;
 
     Board[count1, count2] := Blank;
</delphi>
+
</syntaxhighlight>
 
You can, of course, use three- or higher-dimensional arrays.
 
You can, of course, use three- or higher-dimensional arrays.
  
{|style=color-backgroud="white" cellspacing="20"
+
{{TYNavigator|1-dimensional_arrays|Records}}
|[[1-dimensional_arrays|previous]] 
 
|[[Contents|contents]]
 
|[[Records|next]]
 
|}
 

Latest revision as of 01:18, 21 February 2020

English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

 ◄   ▲   ► 

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

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.

 ◄   ▲   ►