Tweet RSS

Example Delphi 2D Arrays:

Div
unit ProcCounteru;
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, ExtCtrls, StdCtrls, jpeg;

type
  TCounter = Array[1..30] of integer; //The declaration of the array of integers. 
  TFrmProcCount = class(TForm)
    BtnInput: TButton;
    PnlOutIn: TPanel;
    PnlOutCount: TPanel;
    BmbClose: TBitBtn;
    PnlTotal: TPanel;
    ImgBack: TImage;
    procedure BtnInputClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

Const Alpha: array[1..5] of string = ('As','Es','Is','Os','Us');  
//The declaration of a constant array of string.
var
  FrmProcCount: TFrmProcCount;
  Input: String;
  Counter: TCounter;
  Loop: Integer;
implementation
Div

Populating a 2D Array:

Div
procedure TFrmTwoArray.BtnInputClick(Sender: TObject);
Var
  Col: String;
  RowA: String;
begin
      For outer:= 1 to 4 do //The outer loop
        begin
            For Inner:= 1 to 3 do //The inner loop
              Begin
               Col:= Column[Outer];
               RowA:= Row[Inner];
               Input[outer,Inner]:= strtoint(inputbox('Please Enter'+Col,'Rain Fall for'+RowA,'1'));
               //Note that the Var "input" is the name of our 2D array!
           End;
        end;
end;
//The use of an outer/inner loop can be confusing, however with practise one
//can begin to easily see the logic and workings behind it

Starting Intermediate Delphi:

<< Previous Tutorial

Div

Current Tutorial:

</> Download Source Code

Div

Current Tutorial Info:

One can understand the difference between a standard array and a 2D array better by visualizing a grid with n-number or cells on the x-axis and n-number of cells on the y-axis. A 2D array has no definite limit to the amount of columns and rows it can be given, however the larger the array the more data space it will hold in RAM when the application is running. 

When declaring an array of any type (String, Integer, Boolean, Class, etc), this must be declared just under Type and above the form declaration.

Almost always when populating an array one will need a form of a loop  (For loop, While or Repeat) and also a counter variable that records the size of the array.


The outer/inner loop: 

To understand the outer/inner loop take a look at the code used to populate the 2D array. 

A loop is a repetition of code, with an outer/inner loop one can use a second loop (the inner loop) to repeat code a set amount of times, for ever single repeat of the first loop (the outer loop).

For every repeat of the outer loop the code is repeated 3 times due to the inner loop. 

In the this example of how to populate a 2D array the code is repeated 4 times for the outer loop and 3 times in the inner loop thus the code is repeated 3X4 times, thus 12 times.

The power of an outer/inner loop is that for every repetition, the code is slightly different to the previous based on the which loop number of the outer & inner loop the code is on.