Delphi Array Beginner's Story

Source: Internet
Author: User
Tags array length

An array is a list of the same type of data that can be referenced by an index. According to how storage space is obtained, there are two types of arrays supported by Delphi, static arrays and dynamic arrays. The so-called static array is an array type that has been determined at the time of declaration, whereas a dynamic array is an array whose size is not determined at the time of declaration, and the size of the dynamic array is determined when it is used.

Declaring a static array is as simple as specifying the array length and data type, and the following is a declaration of an array to hold the characters:

var hello:array[0..5] of Char;

It is easy to see that the declaration of an array clearly contains 3 parts, and the keyword array indicates that this is an array variable, and [x. Y] indicates that the index of the array is from X to Y, while the of Char indicates that the member of the array is a character type. You can also declare other types of arrays in a similar way, as shown in the following example code:

var scorelist:array[1..25] of Integer;

var namelist:array[1..35] of String;

After an array is declared, the elements (members) of the array can be referenced by index. The method of using array members is similar to using normal variables of this type, and here is a sample code that uses a character array:

var hello:array[0..5] of Char;

Begin
Hello[0]: = ' H ';
HELLO[1]: = ' e ';
HELLO[2]: = ' l ';
HELLO[3]: = ' l ';
HELLO[4]: = ' o ';
HELLO[5]: = CHR (0);
ShowMessage (Hello);
End
Several arrays are declared here, all of which are arrays that contain only one index parameter, that is, one-dimensional arrays. Delphi supports arrays with more index parameters, that is, multidimensional arrays. Here is a sample code that declares a two-dimensional array:
var hello:array[0..5][1..10] of Char;
Accessing a two-dimensional array member is similar to accessing a one-dimensional array, as long as you specify two indexes in a similar way to access the members of a two-dimensional array, the following is a sample code that uses a two-dimensional array:
var hello:array[0..5] of array[0..10] of char;
Begin
Hello[0][0]: = ' H ';
HELLO[0][1]: = ' e ';
HELLO[0][2]: = ' l ';
HELLO[0][3]: = ' l ';
HELLO[0][4]: = ' o ';
HELLO[0][5]: = CHR (0);
ShowMessage (Hello[0]);
End
It is also possible to declare an array with a higher dimension in a similar way, and no instances are given here. In addition, Delphi supports another way to access two-dimensional array members, the following is the sample code:
var hello:array[0..5] of array[0..10] of char;
Begin
hello[0,0]: = ' H ';
hello[0,1]: = ' e ';
hello[0,2]: = ' l ';
hello[0,3]: = ' l ';
hello[0,4]: = ' o ';
hello[0,5]: = CHR (0);
ShowMessage (Hello[0]);
End
The previous only used static arrays, and Delphi also supports dynamic arrays, where the dynamic array is declared without specifying the size of the array, but is directly specified when used, and here is how to define a dynamic array:
var scorelist:array of Integer;
Because the size of the dynamic array is not specified at the time of declaration, the size of the dynamically specified array must be specified before use, which is achieved by calling procedure SetLength, which is an example of specifying the dynamic array size:
Var
I:integer;
Stutcount:integer;
Scorelist:array of Integer;
Begin
Stutcount: = 50;
SetLength (Scorelist, Stutcount);
Set the score list
For I: = 0 to StutCount-1 do
Begin
Scorelist[i]: = Random (100);
End
Print the score list
For I: = 0 to StutCount-1 do
Begin
ShowMessage (' Stut Index: ' + inttostr (I + 1) +
#13 # # + ' Socre Value: ' + inttostr (scorelist[i]);
End
To finalize the score list
SetLength (scorelist, 0);
End
This code first uses the Stutcount variable to initialize the size of the array, then randomly assigns an integer from 0 to 100 for all array members, then outputs the integers using the dialog box, and finally uses SetLength to set the length of the array to 0, freeing up the space occupied by the array. In addition, later chapters will show that a dynamic array is actually a pointer.
4. Address and pointers
When learning Delphi programming, the concept of an address is very important, which is the basis for understanding pointer variables. The so-called address is the location of a memory cell in memory, which is relative to the first storage unit in memory. If the address of the first cell in memory is recorded as 0 and the address of the second cell is 1, the address of the 1000th cell can be recorded as 999. This is, of course, the simplest notation, where the actual address notation is more complex when executing an application, but the basic concepts are identical. In addition, it is important to note that only when the program is loaded into memory to begin execution, all variables in the program have the actual corresponding storage unit in memory, and at this time the memory address is of practical significance, but the programming language has already hidden these underlying complex problems, The program simply uses pointer variables to represent pointers and does not need to be concerned with the actual memory address.
Pointers are the types of variables used to represent memory addresses. In fact, the address of most variables is maintained by the program language itself, and programmers do not need to maintain variables for the addresses of these variables. Pointers are a type of variable that programmers introduce in order to manage certain memory-related variables, which are specifically used to record the address value of the starting address of a storage space in memory, so that the storage space can be easily found and access to its contents.
A dynamic array variable is a pointer-type variable. Arrays are held together in memory, where multiple members of an array are put together in memory in a certain order, and a dynamic array variable is a pointer variable that points to the starting address of the memory, and by invoking the pointer variable, the contents of the array can be accessed, and the usage of the dynamic array variable is given earlier. To provide a more in-depth explanation of this situation, here is a more obvious example:
Var
I:integer;
Stutcount:integer;
Scorelist, Myscorelist:array of Integer;
Begin
Stutcount: = 50;
SetLength (Scorelist, Stutcount);
Set the score list
For I: = 0 to StutCount-1 do
Begin
Scorelist[i]: = Random (100);
End
Print the score list
Myscorelist: = scorelist;
For I: = 0 to StutCount-1 do
Begin
ShowMessage (' Stut Index: ' + inttostr (I + 1) +
#13 # # + ' Socre Value: ' + inttostr (Scorelist[i]) +
#13 # # + ' My socre Value: ' + inttostr (Myscorelist[i])
);
End
To finalize the score list
SetLength (scorelist, 0);
End
It is because the dynamic array variable is just a pointer variable to hold the memory address, so the statement shown in bold above will make the array myscorelist and Scorelist point to the same memory address, thus becoming the same array as the actual meaning. So the effect of this code execution is a powerful illustration of the fact that dynamic array variables are pointers.
Note: But the pointer itself is also a variable, it must also be stored in memory, so there is also a memory address to the variable, and this memory address is maintained by the program language itself, do not need to be managed by the programmer. In fact, as stated earlier, all variables must be stored in memory while the program is running, and there are actual addresses that do not need to be managed by the programmer because the programming language has helped the programmer to complete the task of managing variable addresses.

There are pointer types for all simple data types (integer, real, Boolean, etc.) and a large number of complex data types, and you can use the following sample code if your program needs to use pointer types for these variables:

Var
A:integer;
Pa:pinteger;
Begin
A: = 1;
PA: = @A;
ShowMessage (IntToStr (pa^));
End
As long as the code is entered into the program, it is easy to get the result of the run-that is, the content of the popup dialog box is 1. In fact, the use of pointers is quite complex, and the following code is also correct:
Var
A:integer;
Pa:pinteger;
Begin
A: = 1;
PA: = @A;
pa^: = 2;
ShowMessage (IntToStr (A));
End
In a program, programmers can also define pointer data types for their own defined normal or complex data types, depending on the actual needs, and here's the sample code that defines the pointer data type:
Type
Pmyrec = ^tmyrec;
Tmyrec = Record
P1:integer;
P2:integer;
name:string;
End
As you can see, this defines a struct type and defines its pointer type Pmyrec. In subsequent code, the program can use the data type and pointer data types defined here, and here is a sample code that uses this data type:
Var
Rec:tmyrec;
Mrec:pmyrec;
Begin
Mrec: = @rec;
mrec^. P1: = 12;
mrec^. P2: = 2029;
mrec^. Name: = ' mywork ';
ShowMessage (rec. Name + #13 # # + ' ID: ' +
IntToStr (rec. P1) + ', ' + INTTOSTR (rec. P2));
End

Description: A struct data type is used here, and a method for using struct data types is given, which is no longer described in detail.

Original: http://www.delphitop.com/html/jichu/1895.html

Delphi Array Beginner's Story

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.