Delphi array type

Source: Internet
Author: User
Tags array definition

Array type


The array type defines a sequence of elements of the specified type. You can access the elements in the array by entering the following values in square brackets. When defining an array, square brackets are also used to specify possible subscript values. For example, the following code defines an array with 24 integers:

Type
Daytemperatures = array [1 .. 24]Integer;

When defining an array, you need to fill in a value of the subfield type in square brackets, or use two constants of the ordered type to define a new subfield type, the subfield type specifies the valid index of the array. Since the subfield type specifies the upper and lower bounds of the array lower mark values, the subscript does not have to start from scratch like C, C ++, Java and other languages.

Since the array subscript is based on the subinterface type, Delphi can check their range. An invalid constant quantum field type will result in a compilation time error. If the compiler range check option is selected, an out-of-range value will lead to a running time error.

The preceding array definition method defines a daytemperatures type variable as follows:

Type
Daytemperatures = array [1 .. 24] of integer;

VaR
Daytemp1: daytemperatures;

Procedure assigntemp;
Begin
Daytemp1 [1]: = 54;
Daytemp1 [2]: = 52;
...
Daytemp1 [24]: = 66;
Daytemp1 [25]: = 67; // compile-time error

Arrays can be multidimensional, as shown in the following example:

Type
Monthtemps =Array[1 .. 24, 1 .. 31] of integer;
Yeartemps =Array[1 .. 24, 1 .. 31, Jan .. DEC] of integer;

These two arrays are built on the same core type, so you can declare them with the data types defined above, as shown in the following code:

Type
Monthtemps =Array[1 .. 31] of daytemperatures;
Yeartemps =Array[Jan .. DEC] of monthtemps;

The statement in the preceding example changes the index order, but the entire value can still be assigned between variables. For example, assign the temperature value of January to January:

VaR
Thisyear: yeartemps;

Begin
...
Thisyear [Feb]: = Thisyear [Jan];

You can also define the array whose subscript starts from scratch, but this does not seem logical, because you need to use subscript 2 to access the third item of the array. However, Windows has always followed the array starting from scratch (because it is based on the C language), and the Delphi control library is also moving closer in this direction.

When using an array, you always use the standard functions low and high to detect its boundary. low and high return the lower bound and upper bound of the lower mark. We strongly recommend that you use low and high to operate arrays, especially in loops, because this will make the code irrelevant to the array range. If you change the range declaration under the array, the low and high code will not be affected; otherwise, if the Code contains an array subscript loop body, You have to update the code of the loop body when the array size changes. Low and high make your code easier to maintain and more stable.

Note: By the way, using low and high does not increase additional overhead for system operation. Because they have been converted into constant expressions during compilation, rather than actual function calls. This is also true for other simple system functions.

Delphi mainly uses arrays as array attributes. We have seen the array attribute in the timenow example, that is, the items attribute of The ListBox control. The next chapter will introduce more examples of array attributes when discussing the Delphi loop.

Note: In Object Pascal of Delphi 4, dynamic arrays are added. The so-called dynamic arrays change the array size by dynamically allocating memory at runtime. It is easy to use dynamic arrays, but I think it is inappropriate to discuss these arrays here. You will see the description of Delphi dynamic array in Chapter 8.



From Weizhi note (wiz)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.