Last Updated:

Working with Arrays in Delphi

Considering such an element as an array, it looks like a combination of certain elements (objects) that belong to the same type, and also each of which in the set owns its own index.

Arrays themselves belong to data of a structural type in the form of classes, sets, files, records, and strings. They are quite convenient to use to store a certain type of data, so let's see how to work with arrays.

Array Declaration

Before you start using an array, like any other variable, you must first perform the process of designating them in a special section. If we consider the general view, then this process will look like this:

Name: [lower index ... superscript] of type


In them, it is worth looking at the meaning of the following components:


  • name – the name of a specific array;
  • array – a definition in a programming language that indicates that an array is used;
  • Indexes – constant values that indicate the range within which certain actions occur;
  • type—The type of elements that make up the array.

An example of how the array declaration process occurs can be seen below:

Working with arrays in delphi

As for the process itself, it is quite convenient to use permanent named type for this. This process is carried out in a special section, which is most often located before the section in which variables are declared.

The section itself originates from a special word, which is written as const. In this section, you must specify the name of the constant and the value that it accepts. In this case, these two parameters are separated by the sign "=".

After you declare a constant, you can use it as a symbolic and numeric constant in your program. Below you can see an example of declaring specifically the constants of a named sample:

In the event that in a future program there is a need to use an array element, you need to specify the index of this element and the name of the array itself. The index itself is necessarily taken in square brackets. As for the element that can act as an index, it can be a value of an integer type or a constant. You can look at the following example:

Working with arrays in delphi

In the event that the array is not local, then along with how the process of declaring the array will take place, you can initialize it. This process indicates that specific values are assigned to individual elements of the array. To do this, there is an example:

Depending on what the dimension of the array is, the number of elements that can be initialized will be determined. In the event that this data differs, the compiler will immediately report an error, indicating that the number of elements does not match the number declared in the array.

Operations when working with arrays in Delphi


In the Delphi programming language, the following operations can be performed on arrays:

  • enter an array;
  • Array output
  • search for a specific element in the array;
  • define the maximum and minimum value in the array;
  • Sort the elements of the array.

Let's deal with some operations in more detail.

Entering an Array into Delphi

Considering such a process as entering an array into Delphi, it is worth understanding that this is the process by which specific values of the array elements are obtained from the file or from the user. At the same time, in the process of implementing this action, there is a need to create a specific field for input. But in the event that the work will be performed with a sufficiently large array of data, then it is impractical to use this method.

It is quite obvious that it is much more convenient to carry out the process of entering into a special row of the table. This is where each cell contains the specific value of the array element. In order to perform the input of the array, Delphi can use several components, among which a special place is occupied by Memo and StringGrid.


01.unit getar_;
04.Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
05.Forms, Dialogs, Grids, StdCtrls;
07.TForml = class(TForm]
08.Labell: TLabel;
09.StringGridl; TStringGrid;
10.Buttonl: TButton;
11.Label2: TLabel;
12.procedure ButtorilClicMSender: TObject];
14. {Private declarations }
16. { Public declarations }
19.Forml: TForml;
21. {SR *.dfm}
22.procedure TForml.ButtonlClick(Sender: TObject);
24.a : array[1..5] of integer; array
25.suntrn: integer; sum of elements
: real; arithmetic
mean 27.i: integer/ // index
29.// array input 30.// we believe that if the
cell is empty, then the corresponding
31.// array element is zero
32.for i:= 1 to 5 do
33.if Length(StringGridl.Cells[i-l,0]] 0
34.then a := StrToInt(StringGridl.Cells[i-1,0))
35.else a := 0;
36.// array processing
37.summ := 0;
38.for i :=1 to 5 do
39.summ := summ + a; : = summ / 5;
41.// output of the result
42.Label2.Caption :='Sum of elements: ' + IntToStr(summl + #13+'The arithmetic mean: ' + FloatToStr(sr);
43.end; end.

Array Output in Delphi

You can also perform an operation such as output on arrays. It consists in the fact that in the dialog box on the monitor screen, the process of displaying the values of specific elements of the array is carried out.
In the event that the program needs to simultaneously display all the elements that are part of the array, then a special for design is used for this. At the same time, there is a peculiarity here, since a variable counter acts as an array element here.

01.unit outar_;

03.Windows, Messages, SysOtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
05.TForml = class(TForm)
06.Buttonl: TButton;
07.Label1: TLabel;
08.procedure ButtonlClick(Sender: TObject);
10. { Private declarations }
12. {Public declarations}
15.Form1: TForm1;
17. {$R *,dfm}
19.NT = 5;
20.var array[1..NT] of string[10] =('Zenit1,'Dynamo1,'Rotor1,'Spartak','SKA') ;
22.procedure TForml.ButtonlClick(Sender: TObject);
23.var; 25.i:integer command
list index, array
element number 26.begin
27.// generate a list to display in the form
28.for i:=l to NT do st := st + IntToStr(i)+ ' '+ team +#13;
29.// list
output 30.Label1.Caption := st;

Sorting arrays in Delphi


In order to ensure that the process of working with arrays in the Delphi programming language is not complicated, an additional procedure such as array sorting can be performed. Depending on what the final task is, the method used depends.

In the event that the work occurs with an array of numbers, then in this situation it is recommended to sort in descending or ascending order. For this purpose, the exchange method can be used. In the process of its use, one element is compared with the subsequent ones and they are rearranged in ascending or descending order. Such an algorithm is used to pass through absolutely all elements.

In addition, sorting in Delphi can be implemented through the use of a recursion method. In it, a single reference element is selected, with which the process of comparing all the others takes place. As a result, you can get that on the left side will be the largest elements, and on the right side those elements that have the lowest value. Sorting in this way occurs until there is only one element of the array left.